Class: Jets::Commands::Upgrade

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/commands/upgrade.rb,
lib/jets/commands/upgrade/version1.rb

Defined Under Namespace

Classes: Version1

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Upgrade

Returns a new instance of Upgrade.



6
7
8
# File 'lib/jets/commands/upgrade.rb', line 6

def initialize(options)
  @options = options
end

Instance Method Details

#inject_csrf_meta_tagsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jets/commands/upgrade.rb', line 21

def inject_csrf_meta_tags
  layout = "app/views/layouts/application.html.erb"
  return unless File.exist?(layout)

  lines = IO.readlines(layout)
  csrf_meta_tags = lines.find { |l| l =~ /<%= csrf_meta_tags %>/ }
  return if csrf_meta_tags

  puts "Update: #{layout} with csrf_meta_tags helper"
  lines.map! do |line|
    if line.include?("<head>")
      # add csrf_meta_tags helper
      "#{line}  <%= csrf_meta_tags %>\n"
    else
      line
    end
  end

  content = lines.join
  IO.write(layout, content)
end

#runObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/jets/commands/upgrade.rb', line 10

def run
  puts "Upgrading to Jets structure to latest version"
  Version1.new.run
  # version 2 upgrades
  inject_csrf_meta_tags
  update_crud_js
  update_config_application_rb
  update_autoload_paths_config
  puts "Upgrade complete."
end

#update_autoload_paths_configObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jets/commands/upgrade.rb', line 80

def update_autoload_paths_config
  app_rb = "config/application.rb"
  lines = IO.readlines(app_rb)

  new_config = lines.find { |l| l.include?('config.autoload_paths') }
  return if new_config

  old_config = lines.find { |l| l.include?('config.extra_autoload_paths') }
  return unless old_config

  lines.map! do |line|
    md = line.match(/config\.extra_autoload_paths(.*)/)
    if md
      rest = md[1]
      "  config.autoload_paths#{rest}"
    else
      line
    end
  end

  content = lines.join
  IO.write(app_rb, content)
  puts "Update: #{app_rb} with config.autoload_paths"
end

#update_config_application_rbObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jets/commands/upgrade.rb', line 57

def update_config_application_rb
  app_rb = "config/application.rb"
  lines = IO.readlines(app_rb)
  api_mode = lines.find { |l| l =~ /config\.mode/ && l =~ /api/ }
  return unless api_mode

  forgery = lines.find { |l| l =~ /default_protect_from_forgery/ }
  return if forgery

  lines.map! do |line|
    if line =~ /^end/
      # add default_protect_from_forgery = false
      "  config.controllers.default_protect_from_forgery = false\n#{line}"
    else
      line
    end
  end

  content = lines.join
  IO.write(app_rb, content)
  puts "Update: #{app_rb} with default_protect_from_forgery"
end

#update_crud_jsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jets/commands/upgrade.rb', line 43

def update_crud_js
  src = File.expand_path("templates/webpacker/app/javascript/src/jets/crud.js", File.dirname(__FILE__))
  dest = "app/javascript/src/jets/crud.js"
  return unless File.exist?(dest)

  lines = IO.readlines(dest)
  csrf = lines.find { |l| l =~ /csrf-token/ }
  return if csrf

  FileUtils.mkdir_p(File.dirname(dest))
  FileUtils.cp(src, dest)
  puts "Update: #{dest}"
end