├── .github └── workflows │ └── ci.yml ├── .github_actions_build_config.rb ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── Rakefile ├── TODO ├── build_config.rb ├── example └── create.rb ├── mrbgem.rake ├── mrblib ├── main.rb ├── mrb_mrbgem_template.rb ├── options.rb └── version.rb ├── snapcraft.yaml ├── template_config.rb ├── test ├── mrb_mrbgem_template_test.rb └── options_test.rb └── tools └── mrbgem-template └── mrbgem-template.c /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | on: [push, pull_request] 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | matrix: 8 | mruby_version: ["master", "3.1.0", "2.1.2", "2.0.1"] 9 | env: 10 | MRUBY_VERSION: ${{ matrix.mruby_version }} 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: Install packages 14 | run: | 15 | sudo apt-get -qq update 16 | sudo apt-get -qq install rake bison git gperf 17 | - name: Clone mruby 18 | run: git clone https://github.com/mruby/mruby.git 19 | - name: Copy build file 20 | run: cp -fp ./.github_actions_build_config.rb ./mruby/build_config.rb 21 | - name: Test 22 | run: | 23 | cd mruby 24 | rake all test 25 | 26 | integration-test: 27 | runs-on: ubuntu-latest 28 | strategy: 29 | matrix: 30 | mruby_version: ["master", "3.1.0", "2.1.2", "2.0.1"] 31 | env: 32 | MRUBY_VERSION: ${{ matrix.mruby_version }} 33 | steps: 34 | - uses: actions/checkout@v2 35 | - name: Install packages 36 | run: | 37 | sudo apt-get -qq update 38 | sudo apt-get -qq install rake bison git gperf 39 | - name: Generate mruby-example 40 | run: rake 41 | - name: Build mruby-example 42 | run: cd mruby-example && rake 43 | -------------------------------------------------------------------------------- /.github_actions_build_config.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | toolchain :gcc 3 | conf.gembox 'default' 4 | conf.gem git: 'https://github.com/iij/mruby-io.git' 5 | conf.gem git: 'https://github.com/iij/mruby-dir.git' 6 | conf.gem '../mruby-mrbgem-template' 7 | end 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | mruby/ 2 | mruby-example/ 3 | *.lock 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | mruby-mrbgem-template 2 | 3 | Copyright (c) MATSUMOTO Ryosuke 2013 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | [ MIT license: http://www.opensource.org/licenses/mit-license.php ] 25 | 26 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: 2 | rake 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mruby-mrbgem-template 2 | MrbgemTemplate class 3 | 4 | ## CLI tool 5 | 6 | ```sh 7 | # Build mrbgem-template cli 8 | $ git clone https://github.com/matsumoto-r/mruby-mrbgem-template.git 9 | $ cd mruby-mrbgem-template 10 | $ rake 11 | 12 | # Generate mrbgem using mrbgem-template 13 | $ ./mruby/bin/mrbgem-template -h # see usage 14 | $ ./mruby/bin/mrbgem-template --github-user myusername --author 'My Name' --mruby-version 3.1.0 --bin-name sample-bin mruby-sample # generate sample mrbgem 15 | $ cd mruby-sample 16 | $ rake # build binary 17 | $ ./mruby/bin/sample-bin # execute built binary 18 | ``` 19 | 20 | ## Generate mrbgem using template_config.rb 21 | 22 | ``` 23 | git clone https://github.com/matsumoto-r/mruby-mrbgem-template.git && cd mruby-mrbgem-template/ 24 | ``` 25 | 26 | - edit `template_config.rb` 27 | 28 | ```ruby 29 | params = { 30 | :mrbgem_name => 'mruby-example', 31 | :license => 'MIT', 32 | :github_user => 'your-github-username', 33 | :mrbgem_prefix => File.expand_path('.'), 34 | :class_name => 'Example', 35 | :author => 'your-name', 36 | } 37 | 38 | c = MrbgemTemplate.new params 39 | c.create 40 | ``` 41 | 42 | - run `rake` 43 | 44 | ``` 45 | rake 46 | ``` 47 | 48 | - created template files 49 | 50 | ``` 51 | ls -l ./mruby-example 52 | ``` 53 | 54 | ## install by mrbgems 55 | - add conf.gem line to `build_config.rb` 56 | 57 | ```ruby 58 | MRuby::Build.new do |conf| 59 | 60 | # ... (snip) ... 61 | 62 | conf.gem :git => 'https://github.com/matsumoto-r/mruby-mrbgem-template.git' 63 | end 64 | ``` 65 | ## example 66 | 67 | - input create.rb 68 | 69 | ```ruby 70 | params = { 71 | :mrbgem_name => 'mruby-sample', 72 | :license => 'MIT', 73 | :github_user => 'matsumoto-r', 74 | :mrbgem_prefix => '..', 75 | :class_name => 'Sample', 76 | :author => 'MATSUMOTO Ryosuke', 77 | } 78 | 79 | c = MrbgemTemplate.new params 80 | c.create 81 | ``` 82 | 83 | - output mrbgem template 84 | 85 | ``` 86 | $ ./bin/mruby create.rb 87 | Generate all files of mruby-sample 88 | create dir : ../mruby-sample 89 | create dir : ../mruby-sample/src 90 | create file: ../mruby-sample/src/mrb_sample.c 91 | create file: ../mruby-sample/src/mrb_sample.h 92 | create dir : ../mruby-sample/mrblib 93 | create file: ../mruby-sample/mrblib/mrb_sample.rb 94 | create dir : ../mruby-sample/test 95 | create file: ../mruby-sample/test/mrb_sample.rb 96 | create file: ../mruby-sample/mrbgem.rake 97 | create file: ../mruby-sample/mruby-sample.gem 98 | create file: ../mruby-sample/.github/workflows/ci.yml 99 | create file: ../mruby-sample/.github_actions_build_config.rb 100 | create file: ../mruby-sample/README.md 101 | create file: ../mruby-sample/LICENSE 102 | 103 | > create matsumoto-r/mruby-sample repository on github. 104 | > turn on GitHub Actions on matsumoto-r/mruby-sample repository. 105 | > edit your mruby-sample code, then run the following command: 106 | 107 | cd ../mruby-sample 108 | git init 109 | git add . 110 | git commit -m "first commit" 111 | git remote add origin git@github.com:matsumoto-r/mruby-sample.git 112 | git push -u origin main 113 | 114 | > finally, pull-request mruby-sample.gem to mgem-list https://github.com/bovi/mgem-list 115 | 116 | ``` 117 | 118 | - all methods 119 | 120 | ```ruby 121 | params = { 122 | :mrbgem_name => 'mruby-sample', 123 | :license => 'MIT', 124 | :github_user => 'matsumoto-r', 125 | :mrbgem_prefix => '/usr/local/mrbgems', 126 | :class_name => 'Sample', 127 | :author => 'MATSUMOTO Ryosuke', 128 | } 129 | 130 | c = MrbgemTemplate.new params 131 | 132 | # Create all data 133 | c.create 134 | 135 | # Create root dir 136 | #c.create_root 137 | 138 | # Create src dir and .c .h 139 | #c.create_src 140 | 141 | # Create mrblib dir and .rb 142 | #c.create_mrblib 143 | 144 | # Create mrbgem.rake 145 | #c.create_rake 146 | 147 | # Create gem file 148 | #c.create_mgem 149 | 150 | # Create test dir and .rb 151 | #c.create_test 152 | 153 | # Create .github/workflows/ci.yml and ci.yml config 154 | #c.create_ci 155 | 156 | # Create README.md 157 | #c.create_readme 158 | 159 | # Create LICENS 160 | #c.create_license 161 | 162 | # Output github information 163 | #c.git_info 164 | 165 | # Get data 166 | #puts c.mrblib_data 167 | #puts c.src_c_data 168 | #puts c.src_h_data 169 | #puts c.rake_data 170 | #puts c.mgem_data 171 | #puts c.test_data 172 | #puts c.github_actions_data 173 | #puts c.github_actions_build_config_data 174 | #puts c.readme_data 175 | #puts c.license_data 176 | 177 | # Set data 178 | #c.mrblib_data = "hogehoge" 179 | #. 180 | #. 181 | #. 182 | 183 | # Get init data 184 | #c.mrblib_data = c.mrblib_data_init 185 | #. 186 | #. 187 | #. 188 | 189 | ``` 190 | 191 | 192 | ## License 193 | under the MIT License: 194 | - see LICENSE file 195 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | MRUBY_CONFIG=File.expand_path(ENV["MRUBY_CONFIG"] || "build_config.rb") 2 | TEMPLATE_CONFIG=File.expand_path(ENV["TEMPLATE_CONFIG"] || "template_config.rb") 3 | MRUBY_VERSION=ENV["MRUBY_VERSION"] || "3.1.0" 4 | 5 | file :mruby do 6 | sh "git clone --depth=1 https://github.com/mruby/mruby.git" 7 | if MRUBY_VERSION != "master" 8 | Dir.chdir 'mruby' do 9 | sh "git fetch --tags" 10 | rev = %x{git rev-parse #{MRUBY_VERSION}} 11 | sh "git checkout #{rev}" 12 | end 13 | end 14 | end 15 | 16 | desc "compile binary" 17 | task :compile => :mruby do 18 | sh "cd mruby && rake all MRUBY_CONFIG=\"#{MRUBY_CONFIG}\"" 19 | sh "./mruby/bin/mruby \"#{TEMPLATE_CONFIG}\"" 20 | end 21 | 22 | desc "test" 23 | task :test => :mruby do 24 | sh "cd mruby && rake all test MRUBY_CONFIG=\"#{MRUBY_CONFIG}\"" 25 | end 26 | 27 | desc "cleanup" 28 | task :clean do 29 | sh "cd mruby && rake deep_clean" 30 | end 31 | 32 | task :default => :compile 33 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Add test 2 | -------------------------------------------------------------------------------- /build_config.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | toolchain :gcc 3 | conf.gembox 'full-core' 4 | conf.gem './' 5 | 6 | conf.enable_test 7 | conf.enable_debug if ENV['DEBUG'] 8 | end 9 | -------------------------------------------------------------------------------- /example/create.rb: -------------------------------------------------------------------------------- 1 | params = { 2 | :mrbgem_name => 'mruby-sample', 3 | :license => 'MIT', 4 | :github_user => 'matsumoto-r', 5 | :mrbgem_prefix => '..', 6 | :class_name => 'Sample', 7 | :author => 'MATSUMOTO Ryosuke', 8 | } 9 | 10 | c = MrbgemTemplate.new params 11 | 12 | # Create all data 13 | c.create 14 | 15 | # 16 | # Other methods 17 | # 18 | 19 | # Create root dir 20 | #c.create_root 21 | 22 | # Create src dir and .c .h 23 | #c.create_src 24 | 25 | # Create mrblib dir and .rb 26 | #c.create_mrblib 27 | 28 | # Create mrbgem.rake 29 | #c.create_rake 30 | 31 | # Create .gem 32 | #c.create_mgem 33 | 34 | # Create test dir and .rb 35 | #c.create_test 36 | 37 | # Create .github/workflows/ci.yml and ci.yml config 38 | #c.create_ci 39 | 40 | # Create README.md 41 | #c.create_readme 42 | 43 | # Create LICENS 44 | #c.create_license 45 | 46 | # Output github information 47 | #c.git_info 48 | 49 | # Get data 50 | #puts c.mrblib_data 51 | #puts c.src_c_data 52 | #puts c.src_h_data 53 | #puts c.rake_data 54 | #puts c.mgem_data 55 | #puts c.test_data 56 | #puts c.github_actions_data 57 | #puts c.github_actions_build_config_data 58 | #puts c.readme_data 59 | #puts c.license_data 60 | 61 | # Set data 62 | #c.mrblib_data = "hogehoge" 63 | #. 64 | #. 65 | #. 66 | 67 | # Get init data 68 | #c.mrblib_data = c.mrblib_data_init 69 | #. 70 | #. 71 | #. 72 | -------------------------------------------------------------------------------- /mrbgem.rake: -------------------------------------------------------------------------------- 1 | require_relative "mrblib/version" 2 | 3 | MRuby::Gem::Specification.new('mruby-mrbgem-template') do |spec| 4 | spec.license = 'MIT' 5 | spec.authors = 'MATSUMOTO Ryosuke' 6 | spec.version = MrbgemTemplate::VERSION 7 | spec.bins = %w(mrbgem-template) 8 | 9 | spec.add_dependency('mruby-io') 10 | spec.add_dependency('mruby-dir') 11 | spec.add_dependency('mruby-time') 12 | spec.add_dependency('mruby-optparse', github: 'fastly/mruby-optparse') 13 | end 14 | -------------------------------------------------------------------------------- /mrblib/main.rb: -------------------------------------------------------------------------------- 1 | def __main__(argv) 2 | params = MrbgemTemplate.parse_options!(argv) 3 | c = MrbgemTemplate.new params 4 | 5 | c.create 6 | rescue MrbgemTemplate::IllegalParameterError => e 7 | puts e.message 8 | exit 1 9 | end 10 | -------------------------------------------------------------------------------- /mrblib/mrb_mrbgem_template.rb: -------------------------------------------------------------------------------- 1 | class MrbgemTemplate 2 | 3 | attr_accessor :src_c_data, :src_h_data, :mrblib_data 4 | attr_accessor :test_data, :rake_data, :readme_data, :license_data 5 | attr_accessor :github_actions_data, :github_actions_build_config_data, :mgem_data 6 | 7 | DEFAULT_MRUBY_VERSION = "3.1.0" 8 | 9 | class IllegalParameterError < StandardError; end 10 | 11 | def initialize(params = {}) 12 | 13 | # Required params 14 | # :mrbgem_name => 'mruby-hogehoge', 15 | # :license => 'MIT', 16 | # :github_user => 'matsumoto-r', 17 | 18 | # Optional params (auto complete from Required data) 19 | # :mrbgem_prefix => '.', 20 | # :mrbgem_type => 'class', # not yet 21 | # :class_name => 'Hogehoge', 22 | # :author => 'mruby-hogehoge developers', 23 | # :ci => false, # default to :default, :matrix also available 24 | # :bin_name => 'foocli' | true, # if true,detect bin name by gem name 25 | # :mruby_version => '3.1.0' 26 | 27 | raise IllegalParameterError, "mrbgem_name is required" if params[:mrbgem_name].nil? 28 | raise IllegalParameterError, "license is required" if params[:license].nil? 29 | raise IllegalParameterError, "github_user is required" if params[:github_user].nil? 30 | 31 | @params = params 32 | @params[:mrbgem_prefix] = "." if @params[:mrbgem_prefix].nil? 33 | @params[:author] = "#{@params[:mrbgem_name]} developers" if @params[:author].nil? 34 | @params[:class_name] = @params[:mrbgem_name].split('-')[1].capitalize if @params[:class_name].nil? 35 | @params[:ci] = :default if @params[:ci].nil? 36 | @params[:mruby_version] ||= DEFAULT_MRUBY_VERSION 37 | if @params[:bin_name].is_a?(TrueClass) 38 | @params[:bin_name] = @params[:mrbgem_name].sub(/^mruby-(bin-)?/, "") 39 | end 40 | 41 | raise "not found prefix directory: #{@params[:mrbgem_prefix]}" if ! Dir.exist? @params[:mrbgem_prefix] 42 | @root_dir = @params[:mrbgem_prefix] + "/" + @params[:mrbgem_name] 43 | @src_dir = @root_dir + "/src" 44 | @mrblib_dir = @root_dir + "/mrblib" 45 | @test_dir = @root_dir + "/test" 46 | @ci_dir = @root_dir + "/.github" 47 | @workflow_dir = @ci_dir + "/workflows" 48 | 49 | @src_c_data = src_c_data_init 50 | @src_h_data = src_h_data_init 51 | @mrblib_data = mrblib_data_init 52 | @test_data = test_data_init 53 | @rake_data = rake_data_init 54 | @local_builder_data = builder_data_init 55 | if @params[:ci] 56 | @github_actions_data = github_actions_data_init(@params[:ci]) 57 | @github_actions_build_config_data = github_actions_build_config_data_init 58 | end 59 | @readme_data = readme_data_init 60 | @license_data = license_data_init 61 | @mgem_data = mgem_data_init 62 | @tools_data = tools_data_init 63 | end 64 | 65 | def create 66 | puts "Generate all files of #{@params[:mrbgem_name]}" 67 | create_root 68 | create_src 69 | create_mrblib 70 | create_test 71 | create_rake 72 | create_local_builder 73 | create_mgem 74 | create_ci 75 | create_readme 76 | create_license 77 | create_tools 78 | git_info 79 | end 80 | 81 | def create_root 82 | if ! Dir.exist? @root_dir 83 | Dir.mkdir @root_dir, 0755 84 | puts "create dir : #{@root_dir}" 85 | end 86 | end 87 | 88 | def create_src 89 | create_root 90 | if Dir.exist? @src_dir 91 | puts " skip dir : #{@src_dir}" 92 | else 93 | Dir.mkdir @src_dir, 0755 94 | puts "create dir : #{@src_dir}" 95 | end 96 | File.open("#{@src_dir}/mrb_#{@params[:class_name].downcase}.c", "w") do |file| 97 | file.puts @src_c_data 98 | end 99 | puts "create file: #{@src_dir}/mrb_#{@params[:class_name].downcase}.c" 100 | File.open("#{@src_dir}/mrb_#{@params[:class_name].downcase}.h", "w") do |file| 101 | file.puts @src_h_data 102 | end 103 | puts "create file: #{@src_dir}/mrb_#{@params[:class_name].downcase}.h" 104 | end 105 | 106 | def create_mrblib 107 | create_root 108 | if Dir.exist? @mrblib_dir 109 | puts " skip dir : #{@mrblib_dir}" 110 | else 111 | Dir.mkdir @mrblib_dir, 0755 112 | puts "create dir : #{@mrblib_dir}" 113 | end 114 | File.open("#{@mrblib_dir}/mrb_#{@params[:class_name].downcase}.rb", "w") do |file| 115 | file.puts @mrblib_data 116 | end 117 | puts "create file: #{@mrblib_dir}/mrb_#{@params[:class_name].downcase}.rb" 118 | end 119 | 120 | def create_rake 121 | create_root 122 | File.open("#{@root_dir}/mrbgem.rake", "w") do |file| 123 | file.puts @rake_data 124 | end 125 | puts "create file: #{@root_dir}/mrbgem.rake" 126 | end 127 | 128 | def create_mgem 129 | create_root 130 | File.open("#{@root_dir}/#{@params[:mrbgem_name]}.gem", "w") do |file| 131 | file.puts @mgem_data 132 | end 133 | puts "create file: #{@root_dir}/#{@params[:mrbgem_name]}.gem" 134 | end 135 | 136 | def create_test 137 | create_root 138 | if Dir.exist? @test_dir 139 | puts " skip dir : #{@test_dir}" 140 | else 141 | Dir.mkdir @test_dir, 0755 142 | puts "create dir : #{@test_dir}" 143 | end 144 | File.open("#{@test_dir}/mrb_#{@params[:class_name].downcase}.rb", "w") do |file| 145 | file.puts @test_data 146 | end 147 | puts "create file: #{@test_dir}/mrb_#{@params[:class_name].downcase}.rb" 148 | end 149 | 150 | def create_local_builder 151 | create_root 152 | File.open("#{@root_dir}/Rakefile", "w") do |file| 153 | file.puts @local_builder_data 154 | end 155 | puts "create file: #{@root_dir}/Rakefile" 156 | File.open("#{@root_dir}/.gitignore", "a+") do |file| 157 | file.puts "mruby/" 158 | end 159 | puts "add gitignore entry: #{@root_dir}/.gitignore" 160 | end 161 | 162 | def create_ci 163 | create_root 164 | Dir.mkdir @ci_dir, 0755 unless Dir.exist? @ci_dir 165 | Dir.mkdir @workflow_dir, 0755 unless Dir.exist? @workflow_dir 166 | File.open("#{@workflow_dir}/ci.yml", "w") do |file| 167 | file.puts @github_actions_data 168 | end 169 | puts "create file: #{@root_dir}/.github/workflows/ci.yml" 170 | File.open("#{@root_dir}/.github_actions_build_config.rb", "w") do |file| 171 | file.puts @github_actions_build_config_data 172 | end 173 | puts "create file: #{@root_dir}/.github_actions_build_config.rb" 174 | end 175 | 176 | def create_readme 177 | create_root 178 | File.open("#{@root_dir}/README.md", "w") do |file| 179 | file.puts @readme_data 180 | end 181 | puts "create file: #{@root_dir}/README.md" 182 | end 183 | 184 | def create_license 185 | create_root 186 | File.open("#{@root_dir}/LICENSE", "w") do |file| 187 | file.puts @license_data 188 | end 189 | puts "create file: #{@root_dir}/LICENSE" 190 | end 191 | 192 | def create_tools 193 | create_root 194 | if cmd_name = @params[:bin_name] 195 | path = "#{@root_dir}/tools/#{@params[:bin_name]}/#{@params[:bin_name]}.c" 196 | Dir.mkdir File.dirname(File.dirname path), 0755 unless File.exist?(File.dirname(File.dirname path)) 197 | Dir.mkdir File.dirname(path), 0755 unless File.exist?(File.dirname path) 198 | File.open(path, "w") do |file| 199 | file.puts @tools_data 200 | end 201 | puts "create file: #{path}" 202 | end 203 | end 204 | 205 | def git_info 206 | puts < create #{@params[:github_user]}/#{@params[:mrbgem_name]} repository on github. 209 | > turn on GitHub Actions on #{@params[:github_user]}/#{@params[:mrbgem_name]} repository. 210 | > edit your #{@params[:mrbgem_name]} code, then run the following command: 211 | 212 | cd #{@root_dir} 213 | git init 214 | git add . 215 | git commit -m "first commit" 216 | git remote add origin git@github.com:#{@params[:github_user]}/#{@params[:mrbgem_name]}.git 217 | git push -u origin main 218 | 219 | > finally, pull-request #{@params[:mrbgem_name]}.gem to mgem-list https://github.com/mruby/mgem-list 220 | 221 | DATA 222 | end 223 | 224 | def mrblib_data_init 225 | main_def_if_required = @params[:bin_name] ? (< :mruby do 271 | sh "cd mruby && rake all MRUBY_CONFIG=\#{MRUBY_CONFIG}" 272 | end 273 | 274 | desc "test" 275 | task :test => :mruby do 276 | sh "cd mruby && rake all test MRUBY_CONFIG=\#{MRUBY_CONFIG}" 277 | end 278 | 279 | desc "cleanup" 280 | task :clean do 281 | exit 0 unless File.directory?('mruby') 282 | sh "cd mruby && rake deep_clean" 283 | end 284 | 285 | task :default => :compile 286 | DATA 287 | end 288 | 289 | def test_data_init 290 | < '#{@params[:github_user]}/#{@params[:mrbgem_name]}' 393 | end 394 | ``` 395 | ## example 396 | ```ruby 397 | p #{@params[:class_name]}.hi 398 | #=> "hi!!" 399 | t = #{@params[:class_name]}.new "hello" 400 | p t.hello 401 | #=> "hello" 402 | p t.bye 403 | #=> "hello bye" 404 | ``` 405 | 406 | ## License 407 | under the #{@params[:license]} License: 408 | - see LICENSE file 409 | DATA 410 | end 411 | 412 | def license_data_init 413 | if @params[:license] == "MIT" 414 | 415 | <str = str; 505 | data->len = len; 506 | DATA_PTR(self) = data; 507 | 508 | return self; 509 | } 510 | 511 | static mrb_value mrb_#{@params[:class_name].downcase}_hello(mrb_state *mrb, mrb_value self) 512 | { 513 | mrb_#{@params[:class_name].downcase}_data *data = DATA_PTR(self); 514 | 515 | return mrb_str_new(mrb, data->str, data->len); 516 | } 517 | 518 | static mrb_value mrb_#{@params[:class_name].downcase}_hi(mrb_state *mrb, mrb_value self) 519 | { 520 | return mrb_str_new_cstr(mrb, "hi!!"); 521 | } 522 | 523 | void #{gemname_to_funcname('init')}(mrb_state *mrb) 524 | { 525 | struct RClass *#{@params[:class_name].downcase}; 526 | #{@params[:class_name].downcase} = mrb_define_class(mrb, "#{@params[:class_name]}", mrb->object_class); 527 | mrb_define_method(mrb, #{@params[:class_name].downcase}, "initialize", mrb_#{@params[:class_name].downcase}_init, MRB_ARGS_REQ(1)); 528 | mrb_define_method(mrb, #{@params[:class_name].downcase}, "hello", mrb_#{@params[:class_name].downcase}_hello, MRB_ARGS_NONE()); 529 | mrb_define_class_method(mrb, #{@params[:class_name].downcase}, "hi", mrb_#{@params[:class_name].downcase}_hi, MRB_ARGS_NONE()); 530 | DONE; 531 | } 532 | 533 | void #{gemname_to_funcname('final')}(mrb_state *mrb) 534 | { 535 | } 536 | 537 | DATA 538 | end 539 | 540 | def mgem_data_init 541 | < 559 | #include 560 | #include 561 | #include 562 | #include 563 | 564 | int main(int argc, char *argv[]) 565 | { 566 | mrb_state *mrb = mrb_open(); 567 | mrb_value ARGV = mrb_ary_new_capa(mrb, argc - 1); 568 | int i; 569 | int return_value; 570 | 571 | mrb_gv_set(mrb, mrb_intern_lit(mrb, "$0"), mrb_str_new_cstr(mrb, argv[0])); 572 | for (i = 1; i < argc; i++) { 573 | mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i])); 574 | } 575 | mrb_define_global_const(mrb, "ARGV", ARGV); 576 | 577 | mrb_funcall(mrb, mrb_top_self(mrb), "__main__", 1, ARGV); 578 | 579 | return_value = EXIT_SUCCESS; 580 | 581 | if (mrb->exc) { 582 | mrb_print_error(mrb); 583 | return_value = EXIT_FAILURE; 584 | } 585 | mrb_close(mrb); 586 | 587 | return return_value; 588 | } 589 | DATA 590 | end 591 | 592 | def gemname_to_funcname(suffix) 593 | snaked = @params[:mrbgem_name].gsub('-', '_') 594 | "mrb_#{snaked}_gem_#{suffix}" 595 | end 596 | end 597 | -------------------------------------------------------------------------------- /mrblib/options.rb: -------------------------------------------------------------------------------- 1 | class MrbgemTemplate 2 | def self.parse_options!(argv) 3 | options = { # default value 4 | license: "MIT", 5 | mrbgem_prefix: ".", 6 | github_user: detect_github_user, 7 | author: detect_author, 8 | local_builder: true, 9 | mruby_version: nil, 10 | ci: true, 11 | } 12 | parser = OptionParser.new do |opts| 13 | opts.banner = "Usage: mrbgem-template [options] mrbgem_name" 14 | opts.version = MrbgemTemplate::VERSION 15 | 16 | opts.on("-h", "--help", "Show usage") do |v| 17 | puts opts.help 18 | exit 1 19 | end 20 | 21 | opts.on("-v", "--version", "Show version") do |v| 22 | puts "mrbgem-template version #{opts.version}" 23 | exit 1 24 | end 25 | 26 | opts.on("-l", "--license [LICENSE]", "Set license") do |v| 27 | options[:license] = v 28 | end 29 | 30 | opts.on("-u", "--github-user [USER]", "Set user name on github") do |v| 31 | options[:github_user] = v 32 | end 33 | 34 | opts.on("-p", "--mrbgem-prexif [PREFIX]", "Set prefix dir to mgem project") do |v| 35 | options[:mrbgem_prefix] = v 36 | end 37 | 38 | opts.on("-c", "--class-name [CLASS]", "Set class name") do |v| 39 | options[:class_name] = v 40 | end 41 | 42 | opts.on("-a", "--author [AUTHOR]", "Set the author of this mgem") do |v| 43 | options[:author] = v 44 | end 45 | 46 | opts.on("-m", "--mruby-version [VERSION]", "Set target mruby version") do |v| 47 | options[:mruby_version] = v 48 | end 49 | 50 | opts.on("-B", "--bin-name [BIN_NAME]", "Set and generate binary tools") do |v| 51 | options[:bin_name] = v 52 | end 53 | 54 | opts.on("-b", "--[no-]local-builder", "Enable or disable local builder") do |v| 55 | options[:local_builder] = v 56 | end 57 | 58 | opts.on("-C", "--[no-]ci", "Enable or disable CI by GitHub Actions") do |v| 59 | options[:ci] = v 60 | end 61 | end 62 | 63 | if argv.size <= 0 64 | puts parser.help 65 | exit 1 66 | end 67 | 68 | if argv[-1] !~ /\A-/ 69 | options[:mrbgem_name] = argv.pop 70 | options[:class_name] = to_class_name(options[:mrbgem_name]) # This is also default 71 | end 72 | 73 | parser.parse!(argv) 74 | 75 | options 76 | end 77 | 78 | private 79 | def self.to_class_name(snake) 80 | snake = snake.sub(/\Amruby-/, '') 81 | next_camel_mark = true 82 | snake.chars.inject("") do |d, s| 83 | if next_camel_mark 84 | d += s.upcase 85 | next_camel_mark = false 86 | elsif s !~ /[a-zA-Z0-9]/ 87 | next_camel_mark = true 88 | else 89 | d += s 90 | next_camel_mark = false 91 | end 92 | d 93 | end 94 | end 95 | 96 | def self.detect_github_user 97 | detected = `git config github.user`.chomp 98 | detected.empty? ? nil : detected 99 | end 100 | 101 | def self.detect_author 102 | detected = `git config user.name`.chomp 103 | detected.empty? ? nil : detected 104 | end 105 | end 106 | -------------------------------------------------------------------------------- /mrblib/version.rb: -------------------------------------------------------------------------------- 1 | class MrbgemTemplate 2 | VERSION = '0.3.1' 3 | end 4 | -------------------------------------------------------------------------------- /snapcraft.yaml: -------------------------------------------------------------------------------- 1 | name: mrbgem-template 2 | version: "0.3.0" 3 | summary: mrbgems template generator 4 | description: | 5 | mrbgems template generator 6 | 7 | base: core18 8 | license: MIT 9 | 10 | confinement: strict 11 | 12 | parts: 13 | mrbgem-template: 14 | source: https://github.com/udzura/mruby-mrbgem-template.git 15 | plugin: make 16 | build-packages: 17 | - libruby2.5 18 | - ruby2.5 19 | - ruby2.5-dev 20 | - rake 21 | - git 22 | - gcc 23 | - bison 24 | artifacts: 25 | - mruby/bin/mrbgem-template 26 | apps: 27 | mrbgem-template: 28 | command: mruby/bin/mrbgem-template -------------------------------------------------------------------------------- /template_config.rb: -------------------------------------------------------------------------------- 1 | params = { 2 | :mrbgem_name => 'mruby-example', 3 | :license => 'MIT', 4 | :github_user => 'your-github-username', 5 | :mrbgem_prefix => File.expand_path('.'), 6 | :class_name => 'Example', 7 | :author => 'your-name', 8 | :bin_name => 'foobar' 9 | } 10 | 11 | c = MrbgemTemplate.new params 12 | c.create 13 | -------------------------------------------------------------------------------- /test/mrb_mrbgem_template_test.rb: -------------------------------------------------------------------------------- 1 | ## 2 | ## MrbgemTemplate Test 3 | ## 4 | 5 | assert("MrbgemTemplate#*_data") do 6 | params = { 7 | :mrbgem_name => 'mruby-sample', 8 | :license => 'MIT', 9 | :github_user => 'matsumoto-r', 10 | :mrbgem_prefix => '..', 11 | :class_name => 'Sample', 12 | :author => 'MATSUMOTO Ryosuke', 13 | } 14 | 15 | c = MrbgemTemplate.new params 16 | assert_equal(c.src_c_data, c.src_c_data_init) 17 | assert_equal(c.src_h_data, c.src_h_data_init) 18 | assert_equal(c.mrblib_data, c.mrblib_data_init) 19 | assert_equal(c.rake_data, c.rake_data_init) 20 | assert_equal(c.mgem_data, c.mgem_data_init) 21 | assert_equal(c.test_data, c.test_data_init) 22 | assert_equal(c.license_data, c.license_data_init) 23 | assert_equal(c.readme_data, c.readme_data_init) 24 | assert_equal(c.github_actions_data, c.github_actions_data_init) 25 | assert_equal(c.github_actions_build_config_data, c.github_actions_build_config_data_init) 26 | end 27 | 28 | assert("MrbgemTemplate#*_data=") do 29 | params = { 30 | :mrbgem_name => 'mruby-sample', 31 | :license => 'MIT', 32 | :github_user => 'matsumoto-r', 33 | :mrbgem_prefix => '..', 34 | :class_name => 'Sample', 35 | :author => 'MATSUMOTO Ryosuke', 36 | } 37 | 38 | c = MrbgemTemplate.new params 39 | c.src_c_data = "aaa" 40 | assert_equal("aaa", c.src_c_data) 41 | c.src_h_data = "bbb" 42 | assert_equal("bbb", c.src_h_data) 43 | c.mrblib_data = "ccc" 44 | assert_equal("ccc", c.mrblib_data) 45 | c.rake_data = "rake" 46 | assert_equal("rake", c.rake_data) 47 | c.mgem_data = "mgem" 48 | assert_equal("mgem", c.mgem_data) 49 | c.test_data = "test" 50 | assert_equal("test", c.test_data) 51 | c.license_data = "license" 52 | assert_equal("license", c.license_data) 53 | c.readme_data = "readme" 54 | assert_equal("readme", c.readme_data) 55 | c.github_actions_data = "ci" 56 | assert_equal("ci", c.github_actions_data) 57 | c.github_actions_build_config_data = "build" 58 | assert_equal("build", c.github_actions_build_config_data) 59 | end 60 | 61 | assert("MrbgemTemplate#gemname_to_funcname") do 62 | params = { 63 | :mrbgem_name => 'mruby-sample', 64 | :license => 'MIT', 65 | :github_user => 'matsumoto-r', 66 | :mrbgem_prefix => '..', 67 | :class_name => 'Sample', 68 | :author => 'MATSUMOTO Ryosuke', 69 | } 70 | 71 | c = MrbgemTemplate.new params 72 | assert_equal("mrb_mruby_sample_gem_init", c.gemname_to_funcname('init')) 73 | assert_equal("mrb_mruby_sample_gem_final", c.gemname_to_funcname('final')) 74 | 75 | params = { 76 | :mrbgem_name => 'mruby-complex-sample', 77 | :license => 'MIT', 78 | :github_user => 'matsumoto-r', 79 | :mrbgem_prefix => '..', 80 | :class_name => 'Sample', 81 | :author => 'MATSUMOTO Ryosuke', 82 | } 83 | 84 | c = MrbgemTemplate.new params 85 | assert_equal("mrb_mruby_complex_sample_gem_init", c.gemname_to_funcname('init')) 86 | assert_equal("mrb_mruby_complex_sample_gem_final", c.gemname_to_funcname('final')) 87 | end 88 | -------------------------------------------------------------------------------- /test/options_test.rb: -------------------------------------------------------------------------------- 1 | assert("MrbgemTemplate.parse_options!") do 2 | argv = [ 3 | "-l", "Apache 2.0", 4 | "-u", "matsumoto-r", 5 | "-p", "/path/to/root", 6 | "-c", "FooClass", 7 | "-a", "Ryosuke Matsumoto", 8 | "--no-local-builder", 9 | "--no-ci", 10 | "mruby-buz" 11 | ] 12 | params = MrbgemTemplate.parse_options!(argv) 13 | 14 | assert_equal(params[:license], "Apache 2.0") 15 | assert_equal(params[:github_user], "matsumoto-r") 16 | assert_equal(params[:mrbgem_prefix], "/path/to/root") 17 | assert_equal(params[:class_name], "FooClass") 18 | assert_equal(params[:author], "Ryosuke Matsumoto") 19 | assert_equal(params[:local_builder], false) 20 | assert_equal(params[:ci], false) 21 | assert_equal(params[:mrbgem_name], "mruby-buz") 22 | end 23 | 24 | assert("MrbgemTemplate.parse_options! default values") do 25 | argv = [ 26 | "-u", "dummy-user", 27 | "-a", "Dummy authors", # todo: mock git command response? 28 | "mruby-qaaz_qez" 29 | ] 30 | params = MrbgemTemplate.parse_options!(argv) 31 | 32 | assert_equal(params[:license], "MIT") 33 | assert_equal(params[:github_user], "dummy-user") 34 | assert_equal(params[:mrbgem_prefix], ".") 35 | assert_equal(params[:class_name], "QaazQez") 36 | assert_equal(params[:author], "Dummy authors") 37 | assert_equal(params[:local_builder], true) 38 | assert_equal(params[:ci], true) 39 | assert_equal(params[:mrbgem_name], "mruby-qaaz_qez") 40 | end 41 | -------------------------------------------------------------------------------- /tools/mrbgem-template/mrbgem-template.c: -------------------------------------------------------------------------------- 1 | // This file is generated by mruby-cli. Do not touch. 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /* Include the mruby header */ 9 | #include 10 | #include 11 | #include 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | mrb_state *mrb = mrb_open(); 16 | mrb_value ARGV = mrb_ary_new_capa(mrb, argc); 17 | int i; 18 | int return_value; 19 | 20 | // Not to include argv[0]... 21 | for (i = 1; i < argc; i++) { 22 | mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i])); 23 | } 24 | mrb_define_global_const(mrb, "ARGV", ARGV); 25 | 26 | // call __main__(ARGV) 27 | mrb_funcall(mrb, mrb_top_self(mrb), "__main__", 1, ARGV); 28 | 29 | return_value = EXIT_SUCCESS; 30 | 31 | if (mrb->exc) { 32 | mrb_print_error(mrb); 33 | return_value = EXIT_FAILURE; 34 | } 35 | mrb_close(mrb); 36 | 37 | return return_value; 38 | } 39 | --------------------------------------------------------------------------------