mwc 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 925b00ab93076748302d2dad5679a40efb4f7591665573442575628968412b08
4
- data.tar.gz: 262c0c7220decf69df1df2bb0caa03a27e1ad0acdfed47b9a00a41f0b9b327cd
3
+ metadata.gz: 690be51483f8bb3eb16c1f4a85cfa326bd2602af973c04e06423b7fa445a83db
4
+ data.tar.gz: a43bc9935ac9648f72748f88806926e96cc2a1465f27fc3f9274cb9a40689162
5
5
  SHA512:
6
- metadata.gz: ac13dfcb13e3981b5921f2d164da25ea1b6c2ae8eb7e6e7ca4628b68374b268c916c5db0f38decaca63d864e3d335bdfa4faee01aff571c1a5e27e72bf17edac
7
- data.tar.gz: 41ce0005d7968418b91462788ec2f29030d31f881ff9e8a273aa9ba8a28bfc431e4d12d8caca2d14b21fc4db80462688051504fde0a0f02f72c0f5cdabe0de89
6
+ metadata.gz: 22abdd8b084890f6cac3b7d73fc3e6873cb5e1a43c79aaab5294897cb143e322bb51bc91f5365f9407b748c6afa7442cb916cbdefa56a791ef2e70b5ed2d1e44
7
+ data.tar.gz: e485459b5a4d99302d229a07e533c0a4547866054a3a6319efc134f320d91d41e30d3852c51a916725dc9a1cfc511837fb37b6c85234d2b5096677c2e98da6ab
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mwc (0.2.0)
4
+ mwc (0.3.0)
5
5
  rack (~> 2.0.7)
6
6
  rake (~> 10.0)
7
7
  thor (~> 0.20.3)
data/README.md CHANGED
@@ -37,34 +37,12 @@ Execute below command with your project name:
37
37
 
38
38
  This gem will create a directory `my_mrb` with anything you need to play with mruby on WebAssembly.
39
39
 
40
- ### Configure
41
-
42
- We use the DSL to specify compile behavior, edit `.mwcrc` to change it.
43
-
44
- ### Add some source code
45
-
46
- ```c
47
- // src/main.c
48
-
49
- #include<mruby.h>
50
- #include<mruby/compile.h>
51
- #include<mruby/string.h>
52
-
53
- int main() {
54
- mrb_state* mrb = mrb_open();
55
- mrb_load_string(mrb, "puts 'Hello World'");
56
- mrb_close(mrb);
57
-
58
- return 0;
59
- }
60
- ```
61
-
62
40
  ### Source code detect
63
41
 
64
- * `src/**/*.c` the normal c code
65
- * `src/js/**/*.lib.js` the JavaScript library can call it inside C
66
- * `src/js/**/*.pre.js` the JavaScript prepend WebAssembly JS
67
- * `src/js/**/*.post.js` the JavaScript append WebAssembly JS
42
+ * `src/**/*.c` the normal C code
43
+ * `src/js/**/*.lib.js` the JavaScript library can be called in C
44
+ * `src/js/**/*.pre.js` the JavaScript prepend to WebAssembly JS
45
+ * `src/js/**/*.post.js` the JavaScript append to WebAssembly JS
68
46
 
69
47
  ### Compile
70
48
 
@@ -72,12 +50,14 @@ To compile `*.c` to `.wasm` you have to execute `compile` command:
72
50
 
73
51
  $ mwc compile
74
52
 
53
+ You can specify compile environment to change with different options:
54
+
55
+ $ mwc compile --env=dev
56
+
75
57
  To see more usage with `help` command:
76
58
 
77
59
  $ mwc help compile
78
60
 
79
- > Current only support minimal compile feature, the optimize and source map will be added soon.
80
-
81
61
  ### Serve compiled files
82
62
 
83
63
  The `mwc` has built-in static file server to help preview or debug:
@@ -86,10 +66,33 @@ The `mwc` has built-in static file server to help preview or debug:
86
66
 
87
67
  And then, open the `http://localhost:8080` you will see the Emscripten web shell and `Hello World` is printed.
88
68
 
89
- ## Roadmap
69
+ ## Configure
70
+
71
+ We use DSL to define the compile preferences in `.mwcrc`
72
+
73
+ ```ruby
74
+ project.name = 'mruby'
75
+ mruby.version = '2.1.3'
76
+
77
+ env :dev do
78
+ project.source_map = true
79
+ end
80
+ ```
81
+
82
+ ### Project
83
+
84
+ |Name|Type|Description
85
+ |----|-----------
86
+ |name|string| The project name, will change the generated file name. ex. `mruby.wasm`
87
+ |shell|string| The shell file template, if you want to use your own html template
88
+ |source_map|boolean| Enable source map for debug
89
+ |options|array| Extra compile options. ex. `-s ALLOW_MEMORY_GROWTH=1`
90
+
91
+ ### mruby
90
92
 
91
- * [ ] Refactor compile function
92
- * [ ] Environment-based compile
93
+ |Name|Type|Description
94
+ |----|-----------
95
+ |version|string| The prefer mruby version
93
96
 
94
97
  ## Development
95
98
 
data/lib/mwc/command.rb CHANGED
@@ -13,6 +13,8 @@ module Mwc
13
13
  class Command < Thor
14
14
  include Utils::CommandRegistry
15
15
 
16
+ class_option :env, desc: 'the prefer environment'
17
+
16
18
  desc 'version', 'show version'
17
19
  def version
18
20
  puts Mwc::VERSION
@@ -10,6 +10,7 @@ module Mwc
10
10
  module Commands
11
11
  # Compile mruby to wasm
12
12
  class Compile < Thor::Group
13
+ include Thor::Actions
13
14
  include Utils::Command
14
15
 
15
16
  name 'compile'
@@ -18,8 +19,13 @@ module Mwc
18
19
  add_option :format, default: 'html', enum: %w[html js wasm]
19
20
 
20
21
  def compile
21
- Tasks.new
22
- Rake::Task[parent_options['format']].invoke
22
+ Mwc.use(parent_options['env']) do
23
+ # TODO: Allow change output directory
24
+ empty_directory('dist')
25
+
26
+ Tasks.new
27
+ Rake::Task[parent_options['format']].invoke
28
+ end
23
29
  end
24
30
  end
25
31
  end
@@ -17,7 +17,7 @@ module Mwc
17
17
  argument :name, type: :string, desc: 'project name'
18
18
 
19
19
  def create_project
20
- empty_directory(name)
20
+ directory('app', name)
21
21
  self.destination_root = name
22
22
  Mwc.root = destination_root
23
23
  end
@@ -25,21 +25,13 @@ module Mwc
25
25
  # :nodoc:
26
26
  def create_mwcrc
27
27
  template('mwcrc.erb', '.mwcrc')
28
- end
29
-
30
- # :nodoc:
31
- def setup_project
32
- empty_directory('vendor')
33
- empty_directory('dist')
34
- empty_directory('src/js')
35
- copy_file('config/build.rb')
36
- copy_file('.gitignore')
28
+ Mwc.config = Pathname.new(destination_root).join('.mwcrc')
37
29
  end
38
30
 
39
31
  # :nodoc:
40
32
  def download_mruby
41
- Mwc.config.reload!
42
33
  # TODO: Allow choose download mode
34
+ empty_directory('vendor')
43
35
  inside(mruby_directory.dirname) do
44
36
  run("curl -OL #{archive_url}")
45
37
  run("tar -zxf #{filename}")
@@ -50,8 +42,9 @@ module Mwc
50
42
 
51
43
  private
52
44
 
45
+ # :nodoc:
53
46
  def version
54
- Mwc.config.mruby.version
47
+ Mwc.mruby.version
55
48
  end
56
49
 
57
50
  # :nodoc:
@@ -64,8 +57,9 @@ module Mwc
64
57
  "#{version}.tar.gz"
65
58
  end
66
59
 
60
+ # :nodoc:
67
61
  def mruby_directory
68
- Mwc.config.mruby.path
62
+ Mwc.mruby.path
69
63
  end
70
64
  end
71
65
  end
@@ -32,10 +32,9 @@ module Mwc
32
32
  # @since 0.2.0
33
33
  # @api private
34
34
  def setup_shell
35
- return if Mwc.config.project.shell.nil?
35
+ return if Mwc.project.shell.nil?
36
36
 
37
- path = Mwc.root.join(Mwc.config.project.shell)
38
- @options.push "--shell-file #{path}"
37
+ @options.push "--shell-file #{Mwc.project.shell}"
39
38
  end
40
39
 
41
40
  # Setup source map
@@ -43,7 +42,7 @@ module Mwc
43
42
  # @since 0.2.0
44
43
  # @api private
45
44
  def setup_source_map
46
- return unless Mwc.config.project.source_map
45
+ return unless Mwc.project.source_map
47
46
 
48
47
  @options.push '-g4 --source-map-base /'
49
48
  end
@@ -53,10 +52,10 @@ module Mwc
53
52
  # @since 0.2.0
54
53
  # @api private
55
54
  def setup_extra
56
- return unless Mwc.config.project.options.any?
55
+ return unless Mwc.project.options.any?
57
56
 
58
- Mwc.config.project.options.each do |name, value|
59
- @options.push "-s #{name}=#{value}"
57
+ Mwc.project.options.each do |option|
58
+ @options.push option
60
59
  end
61
60
  end
62
61
 
@@ -88,7 +87,7 @@ module Mwc
88
87
  # @since 0.1.0
89
88
  # @api private
90
89
  def output(format)
91
- @options.push "-o dist/#{Mwc.config.project.name}.#{format}"
90
+ @options.push "-o dist/#{Mwc.project.name}.#{format}"
92
91
  end
93
92
  end
94
93
  end
data/lib/mwc/config.rb CHANGED
@@ -1,50 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'forwardable'
4
- require 'singleton'
5
4
 
6
- require 'mwc/project'
7
- require 'mwc/mruby'
5
+ require 'mwc/environment'
8
6
  require 'mwc'
9
7
 
10
8
  module Mwc
11
9
  # The compile preferences
12
10
  class Config
13
- class << self
14
- extend Forwardable
11
+ extend Forwardable
15
12
 
16
- delegate %i[
17
- exist?
18
- mruby
19
- ] => :instance
20
- end
13
+ # @since 0.3.0
14
+ # @api private
15
+ delegate %i[environments] => :@default
21
16
 
22
- include Singleton
17
+ # @since 0.3.0
18
+ # @api private
19
+ LOCK = Mutex.new
23
20
 
24
- attr_reader :project, :mruby
21
+ # @since 0.3.0
22
+ # @api private
23
+ attr_reader :default
25
24
 
26
25
  # :nodoc:
27
- def initialize
28
- @path = Mwc.root.join('.mwcrc')
29
- @project = Project.new
30
- @mruby = MRuby.new
31
-
26
+ def initialize(path = Mwc.root.join('.mwcrc'))
27
+ @path = Pathname.new(path)
28
+ @default = Environment.new
32
29
  load_config if exist?
33
30
  end
34
31
 
35
- # TODO: Move to DSL module
36
- # Set name
37
- #
38
- # @param name [String|NilClass] the name
39
- #
40
- # @since 0.1.0
41
- # @api private
42
- def name(name = nil)
43
- return @name if name.nil?
44
-
45
- @name = name.to_s
46
- end
47
-
48
32
  # Check config file exists
49
33
  #
50
34
  # @return [TrueClass,FalseClass] exist or not
@@ -59,12 +43,8 @@ module Mwc
59
43
  #
60
44
  # @since 0.1.0
61
45
  # @api private
62
- def reload!
63
- # TODO: Update path when root changed
64
- @path = Mwc.root.join('.mwcrc')
65
- return unless exist?
66
-
67
- load_config
46
+ def reload
47
+ Mwc.config = Mwc.root.join('.mwcrc')
68
48
  end
69
49
 
70
50
  private
@@ -74,8 +54,7 @@ module Mwc
74
54
  # @since 0.1.0
75
55
  # @api private
76
56
  def load_config
77
- # TODO: Improve config DSL
78
- instance_eval(@path.read)
57
+ LOCK.synchronize { @default.instance_eval(@path.read) }
79
58
  end
80
59
  end
81
60
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mwc/options/project'
4
+ require 'mwc/options/mruby'
5
+
6
+ module Mwc
7
+ # The compile environment manager
8
+ #
9
+ # @since 0.3.0
10
+ # @api private
11
+ class Environment
12
+ # @since 0.3.0
13
+ # @api private
14
+ attr_reader :environments, :project, :mruby
15
+
16
+ # @since 0.3.0
17
+ # @api private
18
+ def initialize(parent = nil, &block)
19
+ @parent = parent
20
+ @environments = {}
21
+ @project = Options::Project.new(parent&.project)
22
+ @mruby = Options::MRuby.new(parent&.mruby)
23
+ instance_exec(self, &block) if block_given?
24
+ end
25
+
26
+ # Define new environment
27
+ #
28
+ # @param name [Symbol] the environment name
29
+ # @param block [Proc] the environment config block
30
+ #
31
+ # @since 0.3.0
32
+ # @api private
33
+ def env(name, &block)
34
+ return if @parent
35
+
36
+ @environments[name.to_sym] = Environment.new(self, &block)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mwc/utils/option'
4
+
5
+ module Mwc
6
+ module Options
7
+ # The mruby preference
8
+ class MRuby
9
+ include Utils::Option
10
+
11
+ option :version
12
+ option :path, type: :path, default: 'vendor/mruby'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mwc/utils/option'
4
+
5
+ module Mwc
6
+ module Options
7
+ # The project related options
8
+ class Project
9
+ include Utils::Option
10
+
11
+ option :name
12
+ option :source_map, type: :bool
13
+ option :shell, type: :path
14
+ option :options, array: true
15
+ end
16
+ end
17
+ end
data/lib/mwc/server.rb CHANGED
@@ -17,7 +17,7 @@ module Mwc
17
17
  Rack::Static.new(
18
18
  ->(_) { [404, {}, []] },
19
19
  root: 'dist', # TODO: Set by config
20
- index: "#{Mwc.config.project.name}.html",
20
+ index: "#{Mwc.project.name}.html",
21
21
  urls: [''],
22
22
  header_rules: [
23
23
  [WASM_RULE, WASM_HEADER]
data/lib/mwc/tasks.rb CHANGED
@@ -77,7 +77,7 @@ module Mwc
77
77
 
78
78
  # :nodoc:
79
79
  def mruby_directory
80
- Mwc.config.mruby.path
80
+ Mwc.mruby.path
81
81
  end
82
82
  end
83
83
  end
File without changes
File without changes
@@ -0,0 +1,11 @@
1
+ #include<mruby.h>
2
+ #include<mruby/compile.h>
3
+ #include<mruby/string.h>
4
+
5
+ int main() {
6
+ mrb_state* mrb = mrb_open();
7
+ mrb_load_string(mrb, "puts 'Hello, mruby on WebAssembly'");
8
+ mrb_close(mrb);
9
+
10
+ return 0;
11
+ }
@@ -1,12 +1,15 @@
1
- # vim: set filetype=ruby
1
+ # vi: set ft=ruby :
2
+
3
+ # frozen_string_literal: true
2
4
 
3
5
  # Project settings
4
6
  project.name = '<%= name %>'
5
7
  # project.shell = 'src/shell.html'
6
- # project.source_map = true
7
-
8
- # -s ALLOW_MEMORY_GROWTH=1
9
- # project.option 'ALLOW_MEMORY_GROWTH', '1'
8
+ # project.options '-s ALLOW_MEMORY_GROWTH=1'
10
9
 
11
10
  # mruby settings
12
11
  mruby.version = '2.1.0'
12
+
13
+ env :dev do
14
+ project.source_map = true
15
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mwc
4
+ module Utils
5
+ # Provide Hash-like accessor
6
+ module HashAccessor
7
+ # Hash-like getter
8
+ #
9
+ # @param name [String|Symbol] the option name
10
+ #
11
+ # @since 0.3.0
12
+ # @api private
13
+ def [](name)
14
+ return unless respond_to?(name)
15
+
16
+ send(name)
17
+ end
18
+
19
+ # Hash-like setter
20
+ #
21
+ # @param name [String|Symbol] the option name
22
+ # @param value [Object] the option value
23
+ #
24
+ # @since 0.3.0
25
+ # @api private
26
+ def []=(name, value)
27
+ return unless respond_to?("#{name}=")
28
+
29
+ send("#{name}=", value)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mwc/utils/hash_accessor'
4
+
5
+ module Mwc
6
+ module Utils
7
+ # Extend option class
8
+ module Option
9
+ # :nodoc:
10
+ def self.included(base)
11
+ base.class_eval do
12
+ extend ClassMethods
13
+ include HashAccessor
14
+
15
+ def initialize(parent)
16
+ @parent = parent
17
+ end
18
+ end
19
+ end
20
+
21
+ # :nodoc:
22
+ module ClassMethods
23
+ # Define new options
24
+ #
25
+ # @param name [String] the option name
26
+ #
27
+ # @since 0.3.0
28
+ # @api private
29
+ def option(name, options = {})
30
+ return create_array_option(name, options) if options[:array] == true
31
+
32
+ option_reader(name, options)
33
+ option_writer(name, options)
34
+ end
35
+
36
+ # Cast value to specify type
37
+ #
38
+ # @param value [Object] the origin value
39
+ # @param type [Symbol] the destination type
40
+ #
41
+ # @since 0.3.0
42
+ # @api private
43
+ def cast(value, type)
44
+ return if value.nil?
45
+
46
+ case type
47
+ when :path then Mwc.root.join(value)
48
+ when :bool then value == true
49
+ else value
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def option_reader(name, options = {})
56
+ define_method name do
57
+ instance_variable_get("@#{name}") ||
58
+ @parent&.send(name) ||
59
+ self.class.cast(options[:default], options[:type])
60
+ end
61
+ end
62
+
63
+ def option_writer(name, options = {})
64
+ define_method "#{name}=" do |value|
65
+ instance_variable_set(
66
+ "@#{name}",
67
+ self.class.cast(value, options[:type])
68
+ )
69
+ end
70
+ end
71
+
72
+ def create_array_option(name, options = {})
73
+ define_method name do |value = nil|
74
+ current = instance_variable_get("@#{name}")&.dup || []
75
+ return current.concat(@parent&.send(name) || []).uniq if value.nil?
76
+
77
+ current.push self.class.cast(value, options[:type])
78
+ instance_variable_set("@#{name}", current)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
data/lib/mwc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mwc
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/mwc.rb CHANGED
@@ -7,6 +7,10 @@ require 'mwc/config'
7
7
 
8
8
  # WebAssembly compile tool for mruby
9
9
  module Mwc
10
+ # @since 0.3.0
11
+ # @api private
12
+ LOCK = Mutex.new
13
+
10
14
  # The project root
11
15
  #
12
16
  # @return [Pathname] the root
@@ -38,7 +42,17 @@ module Mwc
38
42
  # @since 0.1.0
39
43
  # @api private
40
44
  def self.config
41
- Mwc::Config.instance
45
+ @config ||= Config.new
46
+ end
47
+
48
+ # Set config
49
+ #
50
+ # @param path [Pathname] the config path
51
+ #
52
+ # @since 0.3.0
53
+ # @api private
54
+ def self.config=(path)
55
+ @config = Config.new(path)
42
56
  end
43
57
 
44
58
  # The thor template source root
@@ -53,4 +67,53 @@ module Mwc
53
67
  .join('mwc', 'templates')
54
68
  .to_s
55
69
  end
70
+
71
+ # Use prefer environment
72
+ #
73
+ # @param name [String] prefer environment
74
+ # @param block [Proc] the block execute under this environment
75
+ #
76
+ # @since 0.3.0
77
+ # @api private
78
+ def self.use(env, &_block)
79
+ LOCK.synchronize do
80
+ @env = env&.to_sym
81
+ yield
82
+ @env = nil
83
+ end
84
+ end
85
+
86
+ # Current environment
87
+ #
88
+ # @see Mwc::Environment
89
+ #
90
+ # @return [Mwc::Environment] the environment
91
+ #
92
+ # @since 0.3.0
93
+ # @api private
94
+ def self.environment
95
+ return config.default if @env.nil?
96
+
97
+ config.environments[@env] || config.default
98
+ end
99
+
100
+ # Current mruby preferences
101
+ #
102
+ # @return [Mwc::Options::MRuby] the mruby options
103
+ #
104
+ # @since 0.3.0
105
+ # @api private
106
+ def self.mruby
107
+ environment.mruby
108
+ end
109
+
110
+ # Current project preferences
111
+ #
112
+ # @return [Mwc::Options::Project] the project options
113
+ #
114
+ # @since 0.3.0
115
+ # @api private
116
+ def self.project
117
+ environment.project
118
+ end
56
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 蒼時弦也
@@ -150,15 +150,19 @@ files:
150
150
  - lib/mwc/commands/server.rb
151
151
  - lib/mwc/compile_options.rb
152
152
  - lib/mwc/config.rb
153
- - lib/mwc/mruby.rb
154
- - lib/mwc/project.rb
153
+ - lib/mwc/environment.rb
154
+ - lib/mwc/options/mruby.rb
155
+ - lib/mwc/options/project.rb
155
156
  - lib/mwc/server.rb
156
157
  - lib/mwc/tasks.rb
157
- - lib/mwc/templates/.gitignore
158
- - lib/mwc/templates/config/build.rb
158
+ - lib/mwc/templates/app/.gitignore
159
+ - lib/mwc/templates/app/config/build.rb
160
+ - lib/mwc/templates/app/src/main.c
159
161
  - lib/mwc/templates/mwcrc.erb
160
162
  - lib/mwc/utils/command.rb
161
163
  - lib/mwc/utils/command_registry.rb
164
+ - lib/mwc/utils/hash_accessor.rb
165
+ - lib/mwc/utils/option.rb
162
166
  - lib/mwc/version.rb
163
167
  - mwasm.gemspec
164
168
  homepage: https://github.com/elct9620/mwc
data/lib/mwc/mruby.rb DELETED
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mwc'
4
-
5
- module Mwc
6
- # MRuby config
7
- class MRuby
8
- attr_accessor :version
9
-
10
- def path
11
- Mwc.root.join('vendor/mruby')
12
- end
13
- end
14
- end
data/lib/mwc/project.rb DELETED
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mwc
4
- # The project settings
5
- class Project
6
- attr_accessor :name, :shell, :source_map
7
- attr_reader :options
8
-
9
- # :nodoc:
10
- def initialize
11
- @options = {}
12
- end
13
-
14
- # Add customize options
15
- #
16
- # @param name [String] the option name
17
- # @param value [String] the option value
18
- #
19
- # @since 0.2.0
20
- # @api private
21
- def option(name, value)
22
- @options[name] = value
23
- end
24
- end
25
- end