Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RubyGems入門

 RubyGems入門

リンカーズさんの社内LT会で発表した内容です。RubyGemsの仕組みと作り方について。

Shinichi Maeshima

December 10, 2018
Tweet

More Decks by Shinichi Maeshima

Other Decks in Technology

Transcript

  1. rubygemsೖ໳

    View Slide

  2. ΈΜͳgem޷͖Ͱ͢ΑͶ??

    View Slide

  3. gemΛ࢖͏ਓ͸ଟ͍͚Ͳ࡞Δਓ͸গͳ͍

    View Slide

  4. gem࡞ΔͷͬͯࢥͬͨΑΓ΋
    ؆୯
    ͳΜͰ͢Αͱ͍͏࿩Λ͠·͢

    View Slide

  5. ࡞Δલʹɺ·ͣ࢓૊
    Έʹ͍ͭͯͬ͘͟Γ
    ղઆ͠·͢

    View Slide

  6. ͦ΋ͦ΋gemͬͯͲ͏͍͏࢓૊Ͱ
    ಈ͍͍ͯΔͷ
    https://github.com/rubygems/rubygems
    » ΈΜͳ͓ͳ͡Έgem installͳͲͷίϚϯυ
    » gemͷrequireͷڍಈΛఏڙ͍ͯ͠Δ

    View Slide

  7. rubygemsΛrequire͢Δͱɺ
    require͕ஔ͖׵ΘΔ
    1.·ͣ͸ී௨ʹrequire($LOAD_PATHΛ୳͢)
    2.ͳʹ΋ݟ͔ͭΒͳ͔ͬͨΒɺաڈʹgem installͨ͠gemΛ୳͠
    ͯɺݟ͔ͭͬͨΒ$LOAD_PATHʹͦͷgemͷpathΛ௥Ճͯ͠
    require͢Δ

    View Slide

  8. rubygems͸ࣗಈͰrequire͞ΕΔ
    » Ruby1.9͔Β
    » ࣗಈͰrequireͨ͘͠ͳ͍ͱ͖͸RubyͷϏϧυ࣌ʹexport
    RUBYOPT='--disable gems'͓ͯ͘͠ඞཁ͕͋ΔΒ͍͠

    View Slide

  9. Ͱ΋๻ͨͪΏΔ;Θ
    Rails࢖͍ͬͯ΄ͱ
    ΜͲgemΛrequire͠
    ͳ͍ΑͶʁ

    View Slide

  10. Bundler

    View Slide

  11. Gemfile

    gem ‘rails’, ‘>~ 5.2.0’
    gem 'bootsnap', '>= 1.1.0', require: false

    View Slide

  12. Bundler.setup && Bundler.require
    ͜ΕΒ͸Rails؀ڥΛϩʔυ͢Δͱ͖ʹඞ࣮ͣߦ͞ΕΔ
    # config/boot.rb
    require 'bundler/setup'
    # config/application.rb
    Bundler.require(*Rails.groups)

    View Slide

  13. Bundler.setup
    Gemfileʹఆٛ͞Ε͍ͯΔgemΛͥΜͿ$LOAD_PATHʹ௥Ճ͢Δ

    View Slide

  14. Bundler.require(*Rails.groups)
    Gemfileʹఆٛ͞Ε͍ͯΔgemΛrequire͢Δɻͨͩ͠
    » Ҿ਺ͷgroupʹଐ͍ͯ͠ͳ͍gem͸require͠ͳ͍
    » gem 'bootsnap', '>= 1.1.0', require: falseΈ͍ͨʹ
    require: falseͱͳ͍ͬͯΔgem͸require͠ͳ͍
    » ։ൃ༻ͱ͔ςετ༻ͷgemͩͱҰ୴require: falseͱ͓͍ͯ͠
    ͯɺಛఆͷίϚϯυͷͱ͖͚ͩ໌ࣔతʹrequire͢Δɺͱ͍͏gem
    ͕࣌ʑ͋Γ·͢

    View Slide

  15. ࣍ʹ࡞Γํ

    View Slide

  16. budler͕gemͷ౔୆࡞Γ༻ͷίϚ
    ϯυΛఏڙ͍ͯ͠Δ
    bundle gem gemͷ໊લ
    ͰOK

    View Slide

  17. ࢼ͠ʹhelloworld gemΛ࡞ͬͯΈ
    ·͢
    bundle gem helloworld
    Creating gem 'helloworld'...
    MIT License enabled in config
    Code of conduct enabled in config
    create helloworld/Gemfile
    create helloworld/lib/helloworld.rb
    create helloworld/lib/helloworld/version.rb
    create helloworld/helloworld.gemspec
    create helloworld/Rakefile
    create helloworld/README.md
    create helloworld/bin/console
    create helloworld/bin/setup
    create helloworld/.gitignore
    create helloworld/.travis.yml
    create helloworld/.rspec
    create helloworld/spec/spec_helper.rb
    create helloworld/spec/helloworld_spec.rb
    create helloworld/LICENSE.txt
    create helloworld/CODE_OF_CONDUCT.md
    Initializing git repo in /Users/shinichi.maeshima/tmp/helloworld
    Gem 'helloworld' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html

    View Slide

  18. ͨ͘͞ΜϑΝΠϧ͕ੜ੒͞Εͨ
    .
    !"" CODE_OF_CONDUCT.md
    !"" Gemfile
    !"" LICENSE.txt
    !"" README.md
    !"" Rakefile
    !"" bin
    # !"" console
    # $"" setup
    !"" helloworld.gemspec
    !"" lib
    # !"" helloworld
    # # $"" version.rb
    # $"" helloworld.rb
    $"" spec
    !"" helloworld_spec.rb
    $"" spec_helper.rb

    View Slide

  19. ཈͓͑ͯ͘΂͖͸͜ͷ̎ͭ
    » lib/helloworld.rb
    » gemΛrequireͨ͠ͱ͖ͷى఺ʹͳΔϑΝΠϧ
    » helloworld.gemspec
    » gemͷઆ໌΍ґଘؔ܎Λఆٛ͢ΔϑΝΠϧ

    View Slide

  20. lib/helloworld.rb

    require "helloworld/version"
    module Helloworld
    def self.say
    puts 'hello world!'
    end
    end

    View Slide

  21. helloworld.gemspec
    ྫɻҰ෦লུͯ͠·͢
    Gem::Specification.new do |spec|
    spec.name = "helloworld"
    spec.version = Helloworld::VERSION
    spec.authors = ["willnet"]
    spec.email = ["[email protected]"]
    spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
    spec.description = %q{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.files = Dir.chdir(File.expand_path('..', __FILE__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
    end
    spec.bindir = "exe"
    spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
    spec.require_paths = ["lib"]
    spec.add_development_dependency "bundler", "~> 1.17"
    spec.add_development_dependency "rake", "~> 10.0"
    spec.add_development_dependency "rspec", "~> 3.0"
    end

    View Slide

  22. TODO: ͱͳ͍ͬͯΔͱ͜ΖΛॻ͍ͯͳ͍ͱౖ
    ΒΕΔͷͰͪΌΜͱॻ͖·͠ΐ͏
    ྫͷsummaryͱdescriptionͱhomepageΛमਖ਼
    Gem::Specification.new do |spec|
    spec.name = "helloworld"
    spec.version = Helloworld::VERSION
    spec.authors = ["willnet"]
    spec.email = ["[email protected]"]
    spec.summary = %q{Hello World}
    spec.description = %q{Hello World gem for sample}
    spec.homepage = “https://github.com/willnet/helloworld”
    spec.license = "MIT"
    spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
    end
    spec.bindir = "exe"
    spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
    spec.require_paths = ["lib"]
    spec.add_development_dependency "bundler", "~> 1.17"
    spec.add_development_dependency "rake", "~> 10.0"
    spec.add_development_dependency "rspec", "~> 3.0"
    end

    View Slide

  23. ґଘ͍ͯ͠Δgem͕͋Δͱ͖͸
    helloworld.gemspecʹॻ͖
    ·͢

    spec.add_dependency ‘rails’
    spec.add_development_dependency ‘gimei’

    View Slide

  24. ϩʔΧϧ؀ڥʹΠϯετʔϧ͠
    ͯಈ࡞Λࢼ͍ͨ͠
    rake install
    require 'helloworld' #=> true
    Helloworld.say #=> nil
    hello world!

    View Slide

  25. ςετͷৄࡉͳॻ͖ํ͸লུ͠
    ·͢
    ςετ༻ͷσΟϨΫτϦ͕ੜ੒͞Ε͍ͯΔͷͰɺ͙͢ʹ͍ͭ΋ͷΑ͏ʹॻ
    ͚·͢

    View Slide

  26. ςετॻ͖·͠ΐ͏
    Ͷ ❤

    View Slide

  27. gem͕׬੒ͨ͠

    View Slide

  28. rubygems.orgʹϦϦʔε͢Δ
    ͧʂ
    ࣄલʹrubygems.orgͰΞΧ΢ϯτ࡞͓ͬͯ͘ඞཁ͋Δ͸ͣ
    rake release

    View Slide

  29. ࣾ಺Ͱ͔͠࢖Θͳ͍gemͬͯͲ͏
    ϗεςΟϯά͢Δͷʁ
    » github΍gitlabʹίʔυΛϗεςΟϯά
    » Gemfileʹ࣍ͷΑ͏ʹॻ͍͓͚ͯ͹OK
    gem ‘helloworld’, git: ‘https://github.com/willnet/helloworld.git'

    View Slide

  30. gemʹ੾Γग़ͤͦ͏
    ͳίʔυɺͲΜͲΜ
    ੾Γग़͍͖ͯ͠·͠
    ΐ͏!!!1

    View Slide