A, development,Ruby
库
- The installation
bundle
gem install bundle
Copy the code
cd
To the desktop
cd /Users/mac/Desktop
Copy the code
1. Create what you want to developRuby
The framework of library
- format
Bundle gem nameCopy the code
- The sample
bundle gem bwgem
Copy the code
- Desktop generated
bwgem
Folder, which automatically generated some files
cd
tobwgem
folder
cd bwgem
Copy the code
- You can use
tree
Tool to view directory structure
MACdeiMac:bwgem mac$ Tree. ├ ─ ─ CODE_OF_CONDUCT. Md ├ ─ ─ Gemfile ├ ─ ─ LICENSE. TXT ├ ─ ─ the README. Md ├ ─ ─ Rakefile ├ ─ ─ bin │ ├ ─ ─ the console │ └ ─ ─ the setup ├── ├─ ├─ bwgem. ├─ bwgem. ├─ bwgem3 directories, 10 files
Copy the code
The tree tool can be installed using Homebrew, execute brew Install tree
- use
Rubymine
Open thebwgem
folder
2, viewGemfile
file
Gemfile
The file contents are as follows
# frozen_string_literal: true
source "https://rubygems.org"
# Specify your gem's dependencies in bwgem.gemspec
gemspec
gem "rake"."~ > 13.0"
gem "rubocop"."~ > 1.7"
Copy the code
- As you can see, in
Gemfile
The loadsgemspec
Theta is generatedbwgem.gemspec
file - In other words, pass
bundle install
The content of the installation is inbwgem.gemspec
Configuration in file
3. Implement custom functions
- To view
lib
In folderbwgem.rb
File, you can see that the module is automatically generatedBwgem
# frozen_string_literal: true
require_relative "bwgem/version"
module Bwgem
class Error < StandardError; end
# Your code goes here...
end
Copy the code
- in
lib
Create files in folderwrite_file.rb
File, and write the following code
module Bwgem
class Writer
def initialize(write_file_path, content)
Write content to the write_file_path file
File.open(write_file_path, "w+") { |f| f.write(content) }
end
end
end
Copy the code
- Then, in
bwgem.rb
In-file importwrite_file.rb
file
# frozen_string_literal: true
require_relative "bwgem/version"
module Bwgem
class Error < StandardError; end
# Your code goes here...
require 'write_file'
end
Copy the code
4, inbin
Create your own tools in the folder
- will
bin
Two files automatically generated in a folderconsole
andsetup
delete
rm -rf console
rm -rf setup
Copy the code
- in
bin
Folder to create their own tools file, I call itbwtest
5, write,gemspec
file
- To view
bwgem.gemspec
The file content
# frozen_string_literal: true
require_relative "lib/bwgem/version"
Gem::Specification.new do |spec|
spec.name = "bwgem"
spec.version = Bwgem::VERSION
spec.authors = ["TODO: Write your name"]
spec.email = ["TODO: Write your email address"]
spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new("> = 2.4.0")
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(? :test|spec|features)/})}end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
end
Copy the code
- According to
cocopods
thecocoapods.gemspecdocumentation
# frozen_string_literal: true
require_relative "lib/bwgem/version"
Gem::Specification.new do |spec|
spec.name = "bwgem"
spec.version = Bwgem::VERSION
spec.authors = ["Bowen"] # author nickname
spec.email = ["Email"] # Author email
spec.summary = "Description"
spec.description = "Detailed description" # Detailed description
spec.homepage = "https://www.baidu.com" # home page
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new("> = 2.4.0") The lowest supported version
spec.files = Dir["lib/**/*.rb"] + %w{ bin/bwtest README.md LICENSE.txt } # Customize files in Ruby libraries
spec.executables = %w{ bwtest } Executable file
spec.require_paths = %w{ lib } # require 'file path'
end
Copy the code
6. Write in a custom toolCustom function
- find
bin
File homebwtest
File, writeCustom function
#! /usr/bin/env ruby
Require_paths = %w{lib}, so you can use 'require' directly to load files in 'lib'
require 'bwgem'
puts "This is a simple Ruby library."
# ARGV: Array of arguments passed in when executing this file
# ARGV[0]: file path
# ARGV[1]: Writes the contents of the file
Bwgem::Writer.new(ARGV[0], ARGV[1])
Copy the code
The function in bwTest is to pass in two parameters when called, the first parameter is the file path and the second parameter is what needs to be written to the file
7. Implement custom tools
- The terminal execution
# cd /Users/mac/Desktop/bwgem
bundle install
Copy the code
- perform
bwgem
file
# In your desktop TXT file, write hello World!
bundle exec bwtest /Users/mac/Desktop/txt 'hello world! '
Copy the code
- Terminal print
This is a simple Ruby libraryCopy the code
- Simultaneous desktop generation
txt
The file, it sayshello world!
Ii. To be developedRuby
Library uploadingRubyGems
1, will beTools developed
Push to remotegit
warehouse
- inYards cloudCreate a
git
warehouse
- In the implementation
bundle gem bwgem
Is automatically generatedgit
warehouse
- Modify the
bwgem.gemspec
file
# frozen_string_literal: true
require_relative "lib/bwgem/version"
Gem::Specification.new do |spec|
spec.name = "bwgem"
spec.version = Bwgem::VERSION
spec.authors = ["bwwen"] # author nickname
spec.email = ["[email protected]"] # Author email
spec.summary = "Description"
spec.description = "Detailed description" # Detailed description
spec.homepage = "https://gitee.com/lingtian.com/bwgem.git" Git repository address
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new("> = 2.4.0") The lowest supported version
spec.files = Dir["lib/**/*.rb"] + %w{ bin/bwtest README.md LICENSE.txt } # Customize files in Ruby libraries
spec.executables = %w{ bwtest } Executable file
spec.require_paths = %w{ lib } # require 'file path'
end
Copy the code
- Change the released version number
- Associate the created remote
git
Repository and push the local code to the remote repository
git remote add origin https://gitee.com/lingtian.com/bwgem.git
git add .
git commit -m 'initialize'
git push --set-upstream origin master
Copy the code
- On the tag
git tag 1.0.0
git push --tags
Copy the code
2, registeredRubyGems
account
- On the siterubygems.org/sign_upRegistered in
RubyGems
account
3. Local installation and developmentRuby
tool
- The terminal execution
# cd /Users/mac/Desktop/bwgem
gem build bwgem.gemspec
Copy the code
- Successful print
Successfully built RubyGem
Name: bwgem
Version: 1.0.0
File: bwgem-1.0.0.gem
Copy the code
- Will be generated
Bwgem - 1.0.0. Gem
file
- local
gem
The installation
gem install ./bwgem-1.0.0.gem
Copy the code
- You can view locally installed Ruby tools:
Bwgem - 1.0.0
- Invoking command uninstallation
gem uninstall bwgem
Copy the code
4. Upload the tool toRubyGems
- To execute commands, procedures need to be entered in
RubyGems
Password at the time of registration
# curl -u RubyGems registered account https://rubygems.org/api/v1/api_key.yaml > ~ /. The gem/credentials
curl -u bwgem https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
Copy the code
chmod 0600 ~/.gem/credentials
Copy the code
- Execute the command that will generate
Bwgem - 1.0.0. Gem
File push toRubyGems
gem push bwgem-1.0.0.gem
Copy the code
- when
Bwgem - 1.0.0. Gem
Be pushed to theRubyGems
And then open it upRubyGems, you can see the picture below
- At this point, you can let others use your own development
Ruby
The library - Install using gem
gem install bwgem
Copy the code
5. Implement the developed Ruby tools
- use
gem
Installation tools
gem install bwgem
Copy the code
- Implement developed Ruby tools
bwtest /Users/mac/Desktop/say.txt 'Hello World! '
Copy the code
- You can see the terminal printing
This is a simple Ruby libraryCopy the code
- Simultaneous desktop generation
say.txt
The file, it saysHello World!
3. Upgrade your Ruby library
- Modify the code as you see fit
- I changed it here
bin
folderbwtest
File implementation
#! /usr/bin/env ruby
Require_paths = %w{lib}, so you can use 'require' directly to load files in 'lib'
require 'bwgem'
require 'pathname'
puts "#{ARGV[0]}writes#{ARGV[1]}"
# ARGV: Array of arguments passed in when executing this file
# ARGV[0]: file path
# ARGV[1]: Writes the contents of the file
Bwgem::Writer.new(ARGV[0], ARGV[1])
Copy the code
- Then change the version number
- Push code to remote
Git
Warehouse and tag it
git add .
git commit -m '3.0.0'
git push origin master
git tag 3.0.0
git push --tags
Copy the code
- Then through the
bwgem.gemset
generateBwgem - 3.0.0. Gem
gem build bwgem.gemspec
Copy the code
- The terminal prints the information after success
Successfully built RubyGem
Name: bwgem
Version: 3.0.0
File: bwgem-3.0.0.gem
Copy the code
- then
Bwgem - 3.0.0. Gem
Pushed to theRubyGems
gen push bwgem-3.0.0.gem
Copy the code