Class: Blix::Cli::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/blix/cli/application.rb

Constant Summary collapse

DIR_LIST =
%w(
  app assets config doc features lib bin spec www
  app/controllers app/managers app/models app/views app/views/layouts
  assets/css assets/js
  config/assets
  features/support
  www/assets
)
FILE_LIST =
%w(
  app/application.rb
  app/controllers/application_controller.rb
  app/views/layouts/application.html.erb
  app/views/welcome.html.erb
  assets/css/application.scss
  assets/js/application.js
  config/application.yml
  features/support/setup.rb
  features/application.feature
  config.ru
  Gemfile
  README.md
  Guardfile
  bin/console
  bin/bundle
)
WEBPACK_FILES =
%w(
  webpack.config.js
  assets/js/application.webpack.js
)
VUE_FILES =
%w(
  assets/js/components/app.vue
)
TEMPLATE_DIR =
File.absolute_path(File.dirname(__FILE__) + '/../../../templates')
VALID_RESPONSE =
['yes','y','no','n']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Application

Returns a new instance of Application.



50
51
52
53
54
# File 'lib/blix/cli/application.rb', line 50

def initialize(root, options={})
  @root = root
  @options = Thor::CoreExt::HashWithIndifferentAccess.new.merge options
  @options[:vuebasic] = options[:vue] && !options[:webpack]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



48
49
50
# File 'lib/blix/cli/application.rb', line 48

def options
  @options
end

#rootObject (readonly)

Returns the value of attribute root.



48
49
50
# File 'lib/blix/cli/application.rb', line 48

def root
  @root
end

Instance Method Details

#check_command(name, version = nil) ⇒ Object

Raises:



102
103
104
105
106
107
# File 'lib/blix/cli/application.rb', line 102

def check_command(name, version=nil)
   std, err, stat = Open3.capture3("#{name} --version")
   raise Error, err unless stat.success?
   raise Error, "need #{name} version #{version} or higher" if version && (std < version)
   true
end

#confirm(message) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/blix/cli/application.rb', line 111

def confirm(message)
  return true if options[:y]
  resp = nil
  loop do
    print message + '? (y/n): '
    resp = VALID_RESPONSE.index $stdin.gets.chomp.strip.downcase
    break if resp
  end
  resp < 2   # the yes responses.
end

#ensure_directory(name) ⇒ Object

ensure that the directory exists.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/blix/cli/application.rb', line 57

def ensure_directory(name)
  path = File.join(root,name)
  if File.exist?(path)
    if File.directory?(path)
      NullOperation.new("have directory   #{path}")
    else
      raise Error, "cannot create directory:#{path}"
    end
  else
    DirectoryOperation.new(path)
  end
end

#ensure_file(name) ⇒ Object

ensure that the file exists.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/blix/cli/application.rb', line 83

def ensure_file(name)
  path = File.join(root,name)
  new_contents = get_template(name)
  if File.exist?(path)
    if File.file?(path)
      old_contents = File.read(path)
      if (old_contents != new_contents) && (options[:overwrite] || confirm("overwrite file #{path}"))
        FileOperation.new(path, new_contents )
      else
        NullOperation.new("have file   #{path}")
      end
    else
      raise Error, "cannot create file:#{path}"
    end
  else
    FileOperation.new(path, new_contents )
  end
end

#frameworkObject

generate a new application framework



164
165
166
167
168
169
# File 'lib/blix/cli/application.rb', line 164

def framework
  ensure_directory('')
  DIR_LIST.sort.each{|p| ensure_directory(p) }
  FILE_LIST.sort.each{|p| ensure_file(p) }
  CommandOperation.new(File.join(root,'bin'), 'chmod a+x *', 'set permissions on bin dir') 
end

#get_template(name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blix/cli/application.rb', line 70

def get_template(name)
  template_name = name.gsub('/','-')
  template_path = File.join(TEMPLATE_DIR,template_name)
  if File.file?( template_path )
    File.read( template_path )
    template = Liquid::Template.parse(File.read( template_path ))
    template.render(options)
  else
    nil
  end
end

#node_package(package) ⇒ Object

add a node package unless it exists.



123
124
125
126
127
128
129
# File 'lib/blix/cli/application.rb', line 123

def node_package(package)
  if File.exist? File.join(root,'node_modules',package)
    NullOperation.new("have node package   #{package}")
  else
    CommandOperation.new(root, "npm install #{package} --save-dev", "install     #{package}")
  end
end

#setupObject

setup the environment for the given options.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/blix/cli/application.rb', line 172

def setup
  framework

  if options[:webpack]
     webpack
  end

  if (options[:'dry-run'])
    puts "dry run .."
    Operation.describe_all
  else
    Operation.execute_all
  end
end

#webpackObject

setup webpack npm init -y # created package.json file npm install webpack webpack-cli –save-dev npm install coffeescript coffee-loader –save-dev



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/blix/cli/application.rb', line 136

def webpack

  check_command("node", '4.0.0')
  check_command("npm", '6.0.0')

  CommandOperation.new(root, 'npm init -y', 'initialise       node.js') unless File.exist?(File.join(root,'package.json'))
  node_package('webpack')
  node_package('webpack-cli')
  node_package('coffeescript')
  node_package('coffee-loader')


  if options[:vue]
    node_package('vue')
    node_package('vue-template-compiler')
    node_package('vue-loader')
    node_package('vue-style-loader')
    node_package('vue-router')
    node_package('css-loader')
    node_package('sass-loader')
    ensure_directory('assets/js/components')
    VUE_FILES.sort.each{|p| ensure_file(p) }
  end

  WEBPACK_FILES.sort.each{|p| ensure_file(p) }
end