Class: Trek::Generators::Upgrade::V2Generator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Helpers
Defined in:
lib/generators/trek/upgrade/v2_generator.rb

Overview

The Trek 2.0 upgrade: switches a host app from the private GitLab packages (git-sourced gem + @etaminstudio/trek on the GitLab npm registry) to the public ones: the trek gem on RubyGems and the unscoped trek package on npmjs.com.

Constant Summary collapse

GEM_REQUIREMENT =

2.0.0 is the first version published to RubyGems + npmjs.com (the public npm "trek" name starts there — see the 2.0.0 changelog entry), hence a hardcoded floor rather than Trek::VERSION.

"~> 2.0"
NPM_REQUIREMENT =
"^2.0.0"

Instance Method Summary collapse

Instance Method Details

#clean_yarnrcObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 46

def clean_yarnrc
  return unless File.exist?(".yarnrc.yml")

  # Drop the etaminstudio scope and the git.etamin.studio registry
  # entries (registry server, auth token, minimal-age-gate exemption).
  # Text-based so the rest of the file keeps its comments and layout,
  # but YAML-checked: parent keys are only removed once a parse proves
  # they are empty, and nothing is written if the result doesn't parse.
  content = File.read(".yarnrc.yml")
    .gsub(%r{  "?//git\.etamin\.studio[^\n]*:\n(?:    [^\n]*\n)*}, "")
    .gsub(/  etaminstudio:\n(?:    [^\n]*\n)*/, "")

  %w[npmRegistries npmScopes].each do |key|
    content.sub!(/^#{key}:\n/, "") if YAML.safe_load(content)&.fetch(key, :absent).nil?
  end
  content.gsub!(/\n{3,}/, "\n\n")

  create_file ".yarnrc.yml", content, force: true
rescue Psych::Exception
  say "Could not safely edit .yarnrc.yml — remove the git.etamin.studio registry " \
    "and the etaminstudio scope manually.", :yellow
end

#install_gemsObject



30
31
32
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 30

def install_gems
  bundle_install
end

#install_npm_packageObject



83
84
85
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 83

def install_npm_package
  install_npm_dependencies({ "trek" => NPM_REQUIREMENT })
end

#preapprove_npm_packageObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 69

def preapprove_npm_package
  return unless File.exist?(".yarnrc.yml")
  return if File.read(".yarnrc.yml").include?("npmPreapprovedPackages")

  append_to_file ".yarnrc.yml", <<~YAML

    # Trek is our own package — exempt it from Yarn's package gates
    # (minimal age, …) so a freshly published release installs without
    # the quarantine delay.
    npmPreapprovedPackages:
      - trek
  YAML
end

#remove_bundler_credentialsObject



26
27
28
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 26

def remove_bundler_credentials
  run "bundle config unset git.etamin.studio"
end

#rename_npm_dependencyObject



34
35
36
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 34

def rename_npm_dependency
  gsub_file "package.json", %r{"@etaminstudio/trek":}, '"trek":'
end

#rewrite_importsObject



38
39
40
41
42
43
44
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 38

def rewrite_imports
  Dir.glob("app/javascript/**/*.js").each do |file|
    next unless File.read(file).include?("@etaminstudio/trek")

    gsub_file file, %r{(["'])@etaminstudio/trek(["'/])}, '\1trek\2'
  end
end

#summaryObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 87

def summary
  say <<~SUMMARY

    The trek gem now comes from RubyGems and the trek npm package from npmjs.com.

    Manual follow-ups:
      * Remove the NPM_AUTH_TOKEN variable and the Bundler credentials for
        git.etamin.studio (BUNDLE_GIT__ETAMIN__STUDIO — spelled
        BUNDLE_GIT__ETAMINSTUD__IO on apps created before the domain
        migration) from your CI settings, and drop any local
        TREK_DEPLOY_TOKEN export.
      * Revoke this project's Trek deploy token on GitLab.
  SUMMARY
end

#switch_gemfile_to_rubygemsObject



22
23
24
# File 'lib/generators/trek/upgrade/v2_generator.rb', line 22

def switch_gemfile_to_rubygems
  gsub_file "Gemfile", /^gem ["']trek["'],.*$/, %(gem "trek", "#{GEM_REQUIREMENT}")
end