Unlike most introductory tutorials, this one will share some of the basics of getting started with Ruby and scripting. I hope I can give some help to Ruby’s children who also want to get into the pit. If there are any mistakes, PLEASE correct them.
Introduction to Ruby
Ruby official tutorial
We recommend an official Ruby tutorial that can be learned and practiced online: tryruby.org/levels/1/ch…
bundler
Bundler is a tool for managing Ruby gems. Similar to Cocoapods, take a look at the following example:
Source 'http://ruby.taobao.org' gem 'cocoapods', '0.37.2' gem 'fastlane', '~>1.4.0'Copy the code
😂 is exactly the same, frightening.
The instructions for installation and execution are also highly consistent:
sudo gem install bundler
Copy the code
bundle install
Copy the code
The gemfile. lock file will also be generated after bundle install.
Ruby static code analysis
Now that we’ve decided to write Ruby, it’s important to learn how to write code scientifically as well. After being educated at @DraP, We learned about RuboCop, a tool that analyzes non-conforming Ruby code and features similar to OCLint.
The installation
gem install rubocop
Copy the code
configuration
Add the following line to your project’s Gemfile.
Gem 'rubocop', '~> 0.49.1', require: falseCopy the code
detection
Go to the project root directory and execute the rubocop directive.
Auto repair
In addition to detecting nonconforming parts of Ruby code, RuboCop can also fix nonconforming parts of code with the RoboCop -A instruction.
As a side dish chicken, let’s use it briefly.
IDE
For IDE, we chose Atom because we have only used Atom so far, so we can’t analyze its advantages and disadvantages compared to other Ruby editors. Let’s share a few plug-ins that I think are more useful.
sync-settings
This plugin, recommended by @Frostgod, is mainly used to synchronize Atom configuration information. With a few simple configurations, you can quickly deploy your familiar development environment on a new computer.
About the sync – the use of the Settings, you can refer to this article: www.jianshu.com/p/bd006b349… It’s very complete.
platformio-ide-terminal
This plugin lets you run Terminal directly from Atom.
Object-oriented Ruby
One of the most important things for beginners to Ruby is not to write Ruby like a Shell script, but to design with an object-oriented mind.
Auxiliary scripting
According to the content sorted out in the previous article of iOS engineering Automation – Thinking arrangement, we have completed three functions of one-click new feature, one-click switch debugging mode and one-click switch submission mode, and opened source on BigKeeper. The design idea and implementation can be seen directly through the code, so I will not repeat the details. These scripts are also in the process of continuous improvement, where there is a problem, you are welcome to directly comment on the correction, thanks ~
Here are some of the things I learned during scripting.
Some of the harvest
Git enhancement library: Git-flow
As mentioned in the previous article, we followed the git-flow standard, so we needed to find a git-flow support library when writing these helper scripts. Here we use nvie’s library: github.com/nvie/gitflo… .
The installation
Use Homebrew.
brew install git-flow
Copy the code
Basic usage
Initialize the Git-flow repository
git flow init [-d]
Copy the code
Show/Start/end the feature
git flow feature
git flow feature start <name> [<base>]
git flow feature finish <name>
Copy the code
Note: The
parameter must be a COMMIT record for the Develop branch.
Push feature branch to remote repository/pull feature branch from remote repository
git flow feature publish <name>
git flow feature pull <remote> <name>
Copy the code
Show/start/end release
git flow release
git flow release start <release> [<base>]
git flow release finish <release>
Copy the code
Note: The
parameter must be a COMMIT record for the Develop branch.
Show/start/end hotfix
git flow hotfix
git flow hotfix start <release> [<base>]
git flow hotfix finish <release>
Copy the code
Note: The
parameter must be a COMMIT record for the Master branch.
Show/Start support
git flow support
git flow support start <release> <base>
Copy the code
Note: The
parameter must be a COMMIT record for the Master branch.
Command-line argument parser library – OptionParser
A command line parameter parsing library, just a few simple lines of code, can complete command prompt, parsing, error, and other functions. Here’s an example I wrote:
def switch_to_debug_parser
params = {}
OptionParser.new do |opts|
opts.banner = 'Here is help messages of the switch to debug command.'
params[:mainpath] = '/'
opts.on('-m'.'--mainpath=MainPath'.'Path of the main project, end with /') do |main_path|
params[:main_path] = main_path
end
opts.on('-n'.'--modulename=ModuleName'.'Name of the module in Podfile') do |module_name|
params[:module_name] = module_name
end
opts.on('-p'.'--modulepath=ModulePath'.'Path of the module project') do |module_path|
params[:module_path] = module_path
end
end.parse!
Throw an exception if there are no required arguments
raise OptionParser::MissingArgument if params[:module_name].nil?
raise OptionParser::MissingArgument if params[:module_path].nil?
params
end
Copy the code
String splicing
We can concatenate the string with %q or %q without adding \ before the ‘and’ characters as follows:
%Q( pod #{module_name}, :git => '#{source.base}', :branch => '#{source.addition}')
Copy the code
Execute Shell instructions
There are a number of ways Ruby can execute a Shell. I chose IO:
IO.popen(%Q(cd #{path}; git flow feature start #{feature_name})) { |f| puts f.gets }
Copy the code
If the output of the Shell instruction is multiple lines, the IO pipe should be closed prematurely, and the output of the Shell instruction should be printed as follows.
IO.popen(%Q(pod install --project-directory=#{main_path})) { |io| io.each do |line| puts line end } Copy the code
Search and modify file contents
If we need to file for replacement by line search and content, there will be a small hole: if we directly in the current file directly, when the content of the update is not a line of the file. The each_line do | line | can appear abnormal. We solve this problem by introducing temporary files, just as we iterate through the array to insert or delete intermediate elements. As follows:
def find_and_replace(podfile, module_name, module_type, source)
temp_file = Tempfile.new('.Podfile.tmp')
begin
File.open(podfile, 'r') do |file|
file.each_line do |line|
if line.include? module_name temp_file.puts generate_module_config(module_name, module_type, source)else
temp_file.puts line
end
end
end
temp_file.close
FileUtils.mv(temp_file.path, podfile)
ensure
temp_file.close
temp_file.unlink
end
end
Copy the code
Relative path to absolute path
Ruby comes with this functionality, using the file.expand_path method.
The resources
The Atom using tutorial: wiki.jikexueyuan.com/project/ato…
ATOM sync plug-in and configuration, use sync-settings:www.jianshu.com/p/bd006b349…