Class: Neutron::Generator

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/neutron/generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Returns a new instance of Generator.



14
15
16
17
# File 'lib/neutron/generator.rb', line 14

def initialize(options)
  super()
  @options = options
end

Class Method Details

.source_rootObject



19
20
21
# File 'lib/neutron/generator.rb', line 19

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#copy_templateObject



28
29
30
31
32
33
34
35
36
# File 'lib/neutron/generator.rb', line 28

def copy_template
  directory(TEMPLATE_PATH, options[:path], exclude_pattern: /components/)
  if options[:react]
    directory(File.join(TEMPLATE_PATH, 'src/assets/javascripts/components'), File.join(options[:path], 'src/assets/javascripts/components'))
  end
  inside(File.join(options[:path], 'src')) do
    system 'bundle install'
  end
end

#generate!Object



23
24
25
26
# File 'lib/neutron/generator.rb', line 23

def generate!
  self.copy_template
  self.generate_package_json
end

#generate_package_jsonObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/neutron/generator.rb', line 38

def generate_package_json
  say_status :generate, File.join(options[:path], 'src', 'package.json')
  inside(File.join(options[:path], 'src')) do
    # Init NPM
    `npm init -y`
    # Install local neutron npm package
    system "npm install --save-dev #{NEUTRON_NPM_PACKAGE_PATH}"
    # Install NPM packages
    %w(electron electron-prebuilt-compile).each do |package|
      system "npm install --save-dev #{package}"
    end
    if options[:jquery] || options[:bootstrap]
      system "npm install --save jquery"
    end
    if options[:bootstrap]
      system "npm install --save bootstrap"
    end
    if options[:react]
      system "npm install --save react"
      system "npm install --save react-dom"
    end
    # Edit package.json
    username = Etc.getlogin.downcase
    json = JSON.parse(File.read('package.json'))
    json['name'] = options[:name]
    json['description'] = 'Another Neutron app'
    json['repository'] = "https://github.com/#{username}/#{options[:name]}"
    json['author'] = username
    json['license'] = 'MIT'
    json['main'] = 'main_window.js'
    json['scripts']['boot'] = './node_modules/.bin/electron .'
    json['scripts']['buildjs'] = 'NODE_ENV=production ./node_modules/.bin/electron-compile .'
    #json['babel'] = {'presets' => ['es2015']}
    File.open('package.json', 'w') {|f| f << json.to_json}
  end
end