A, development,Ruby

  • The installationbundle
gem install bundle 
Copy the code
  • cdTo the desktop
cd /Users/mac/Desktop
Copy the code

1. Create what you want to developRubyThe framework of library

  • format
Bundle gem nameCopy the code
  • The sample
bundle gem bwgem
Copy the code
  • Desktop generatedbwgemFolder, which automatically generated some files

  • cdtobwgemfolder
cd bwgem
Copy the code
  • You can usetreeTool 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

  • useRubymineOpen thebwgemfolder

2, viewGemfilefile

  • GemfileThe 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, inGemfileThe loadsgemspecTheta is generatedbwgem.gemspecfile
  • In other words, passbundle installThe content of the installation is inbwgem.gemspecConfiguration in file

3. Implement custom functions

  • To viewlibIn folderbwgem.rbFile, 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
  • inlibCreate files in folderwrite_file.rbFile, 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, inbwgem.rbIn-file importwrite_file.rbfile
# 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, inbinCreate your own tools in the folder

  • willbinTwo files automatically generated in a folderconsoleandsetupdelete
rm -rf console
rm -rf setup
Copy the code
  • inbinFolder to create their own tools file, I call itbwtest

5, write,gemspecfile

  • To viewbwgem.gemspecThe 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 tococopodsthecocoapods.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

  • findbinFile homebwtestFile, 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
  • performbwgemfile
# 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 generationtxtThe file, it sayshello world!

Ii. To be developedRubyLibrary uploadingRubyGems

1, will beTools developedPush to remotegitwarehouse

  • inYards cloudCreate agitwarehouse

  • In the implementationbundle gem bwgemIs automatically generatedgitwarehouse

  • Modify thebwgem.gemspecfile
# 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 remotegitRepository 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, registeredRubyGemsaccount

  • On the siterubygems.org/sign_upRegistered inRubyGemsaccount

3. Local installation and developmentRubytool

  • 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 generatedBwgem - 1.0.0. Gemfile

  • localgemThe 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 inRubyGemsPassword 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 generateBwgem - 1.0.0. GemFile push toRubyGems
gem push bwgem-1.0.0.gem
Copy the code
  • whenBwgem - 1.0.0. GemBe pushed to theRubyGemsAnd then open it upRubyGems, you can see the picture below

  • At this point, you can let others use your own developmentRubyThe library
  • Install using gem
gem install bwgem
Copy the code

5. Implement the developed Ruby tools

  • usegemInstallation 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 generationsay.txtThe file, it saysHello World!

3. Upgrade your Ruby library

  • Modify the code as you see fit
  • I changed it herebinfolderbwtestFile 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 remoteGitWarehouse 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 thebwgem.gemsetgenerateBwgem - 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
  • thenBwgem - 3.0.0. GemPushed to theRubyGems
gen push bwgem-3.0.0.gem
Copy the code