#+qiita_teams: 5e172c4b5167abe35a3e #+qiita_private: 6487b0b54e4eda26e9f6 #+OPTIONS: ^:{} #+STARTUP: indent nolineimages #+TITLE: my_help 開発メモ #+AUTHOR: Shigeto R. Nishitani #+EMAIL: (concat "shigeto_nishitani@mac.com") #+LANGUAGE: jp #+OPTIONS: H:4 toc:t num:2 # +OPTIONS: toc:nil #+TAG: ruby, test, my_help #+TWITTER: off #+SETUPFILE: https://fniessen.github.io/org-html-themes/org/theme-readtheorg.setup * <2023-01-06 Fri> get_configの置き場所 thorのsubcommandを使って * <2023-01-05 Thu> cli directory指定 ** directory指定 テスト環境と本番環境でのhelp_dirあるいはconfig_dir指定の 差異の吸収メモ. - --help_dirでcliはhelp_dirを指定できる. - 全て変更したはず. - configが混乱しやすい...config[:ext]なんかを自動で返すといいが. - rspec/unitでは - config_spec.rb, modify_spec.rbではtmp_confをtemp_dirに作成 - list_spec.rbではtemplates_pathを絶対パスで参照 ** Thorでのdirectory指定の変更 Thorのdirectory指定をしっかりやってみる. #+begin_src bash bundle exec rspec --format documentation -e list spec/cli_spec.rb --help_dir='./tmp/.my_Help' invalid option: --help_dir=./tmp/.my_Help Please use --help for a listing of valid options #+end_src これはだめ.なぜなら,--help_dirはmy_helpのlistのoptionであって, rspecは解釈してくれない. ここは,cli_spec.rbで #+begin_src ruby temp_dir = Dir.pwd let(:help_name) { "example" } let(:help_dir) { File.join(temp_dir, "lib", "templates") } it "list with name" do run_command("my_help list #{help_name} a_item --help_dir=#{help_dir}") stop_all_commands expect(last_command_started).to have_output(/a_item/) end #+end_src とすべき. そうすると #+begin_src bash bundle exec exe/my_help list --help_dir='/Users/bob/git_hub/my_help/lib/templates' #+end_src でも動く. そうしておいて,optionsを移動. 今は, #+begin_src bash > bundle exec exe/my_help help list Usage: my_help list [HELP] [ITEM] Options: [--help-dir=HELP_DIR] [--layer=N] list helps #+end_src となっていたが, #+begin_src bash bundle exec exe/my_help Commands: my_help delete [HELP] # delete HELP my_help edit [HELP] # edit help ... Options: [--help-dir=HELP_DIR] #+end_src とclass_optionとすることで,共通して指定できるように変更. * <2022-12-15 Thu> subcommand and options - [[https://hawksnowlog.blogspot.com/2020/09/ruby-thor-tutorial.html#hello-thor][Ruby thor で CLI ツールの開発に入門してみる]] * <2022-12-15 Thu> rspecでerror Arubaでrspecするとpwd directoryが/tmp/arubaになる? そうすると今のままではrspecが通らなくなる. example2.orgの置き場が不定. spec側で強引に作るか... それもsmartにしたいよね. newでは後ろにoptionがあって,#{temp_dir}での作成とできるが, これをその他のoptionsでやるのは難しそう. ** 解決!! Arubaだけの問題なので,テストの時用にhelp_dirを Thorのoptionとして引けるようにした. #+begin_src ruby -n desc "list [HELP] [ITEM]", "list helps" option :help_dir, :type => :string # use method_options [[https://github.com/rails/thor/wiki/Method-Options]] def list(*args) config = get_config(args).config help_dir = options["help_dir"] || config[:local_help_dir] puts List.new(help_dir, config[:ext]).list(*args.join(" ")) end ... context "list command" do temp_dir = Dir.pwd let(:help_name) { "example" } let(:help_dir) { File.join(temp_dir, "lib", "templates") } it "list with name" do run_command("my_help list #{help_name} a_item --help_dir=#{help_dir}") stop_all_commands expect(last_command_started).to have_output(/a_item/) end end #+end_src * <2021-10-18 月> conf_pathの追加 my_help.confの場所を指定するconf_pathをnewで指定できるようにする. #+begin_src diff -n -i > git diff --- a/lib/my_help/my_help_controll.rb +++ b/lib/my_help/my_help_controll.rb @@ -5,13 +5,14 @@ require 'yaml' module MyHelp class Control attr_accessor :local_help_dir, :editor - def initialize() + def initialize(conf_path=nil) # for configuration setups # see https://stackoverflow.com/questions/6233124/where-to-place-access-config-file-in-gem - + @conf_path = conf_path || ENV['HOME'] @template_dir = File.expand_path("../../templates", __FILE__) @exe_dir = File.expand_path("../../exe", __FILE__) - @local_help_dir = File.join(ENV['HOME'],'.my_help') + @local_help_dir = File.join(@conf_path, '.my_help') + @conf_file = File.join(@local_help_dir, '.my_help_conf.yml') @editor = ENV['EDITOR'] || 'emacs' #'code', 'emacs' #'vim' #default editor # @mini_account = File set_help_dir_if_not_exists @@ -28,9 +29,6 @@ module MyHelp end def load_conf - file_name = '.my_help_conf.yml' - # @conf_file = File.join(Dir.pwd, file_name) - @conf_file = File.join(@local_help_dir, file_name) begin conf = YAML.load_file(@conf_file) @editor = conf[:editor] #+end_src 変更箇所は次の通り. - initializeはconf_pathを受け取るが,指定されてない時はnil - これを@conf_pathに受け渡すが,defaultをENV['HOME']に指定 - これでデフォルトのconfigurationを変更していく やり方は, [[https://stackoverflow.com/questions/6233124/where-to-place-access-config-file-in-gem][Where to place/access config file in gem?]] を参照する. これによってTest::unitでのtest環境独自のmy_help環境を構築することが できる. * <2021-11-08 月> conf_pathをactivate testが進んできたので,conf_pathをactivateした. そのためのテストが必要かも. 1. ENV['HOME']で指定した時と... 1. pwdで何もない時のoptionsがどうなっている? #+begin_src yaml config: :template_dir: "/Users/bob/git_hub/my_help/lib/templates" :local_help_dir: "/Users/bob/git_hub/my_help/test/.my_help" :conf_file: "/Users/bob/git_hub/my_help/test/.my_help/.my_help_conf.yml" :editor: emacs #+end_src * <2021-11-08 月> bundle exec exe/my_help editでエラー #+begin_src shell Could not find ffi-1.15.4 in any of the sources Run `bundle install` to install missing gems. #+end_src とのエラー. なんだろう. さらに,command_lineで呼ぶと, : emacs: standard input is not a tty というエラーが出る.これまたなんだろう???