Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gems.rake « tasks « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc70048ea6da59b39a38651a20f5c091fe3cc371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

namespace :gems do
  # :nocov:
  namespace :error_tracking_open_api do
    desc 'Generate OpenAPI client for Error Tracking'
    task :generate do |task|
      # Configuration
      api_url = 'https://gitlab.com/gitlab-org/opstrace/opstrace/-/raw/main/go/pkg/errortracking/swagger.yaml'
      gem_name = 'error_tracking_open_api'
      module_name = 'ErrorTrackingOpenAPI' # Namespacing is not supported like `ErrorTracking::OpenAPI`
      docker_image = 'openapitools/openapi-generator-cli:v6.0.0'

      vendor_gem_dir = Pathname.new(root_directory)
      gem_dir = vendor_gem_dir / gem_name

      # Always start with a clean state.
      rm_rf(gem_dir)

      generate_gem(
        vendor_gem_dir: vendor_gem_dir,
        api_url: api_url,
        gem_name: gem_name,
        module_name: module_name,
        docker_image: docker_image
      )

      post_process(gem_dir: gem_dir, gem_name: gem_name, task: task)
    end

    def root_directory
      File.expand_path('../../vendor/gems', __dir__)
    end

    def generate_gem(vendor_gem_dir:, api_url:, gem_name:, module_name:, docker_image:)
      user_id = File.stat(vendor_gem_dir).uid

      Kernel.system('docker', 'run',
                    "--user=#{user_id}", '--rm', "--volume=#{vendor_gem_dir}:/code", docker_image,
                    'generate',
                    '--input-spec', api_url,
                    '--generator-name', 'ruby',
                    '--output', "/code/#{gem_name}",
                    "--additional-properties=moduleName=#{module_name}"
      )
    end

    def post_process(gem_dir:, gem_name:, task:)
      write_file(gem_dir / 'README.md') do |content|
        readme_banner(task) + content
      end

      write_file(gem_dir / 'LICENSE', license)
      write_file(gem_dir / "#{gem_name}.gemspec") do |content|
        replace_string(content, 'Unlicense', 'MIT')
        replace_string(content, /(\.files\s*=).*/, '\1 Dir.glob("lib/**/*")')
        replace_string(content, /(\.test_files\s*=).*/, '\1 []')
      end

      remove_entry_secure(gem_dir / 'Gemfile')
      remove_entry_secure(gem_dir / '.rubocop.yml')
      remove_entry_secure(gem_dir / '.travis.yml')
      remove_entry_secure(gem_dir / 'git_push.sh')
      remove_entry_secure(gem_dir / 'spec')
      remove_entry_secure(gem_dir / '.rspec')
    end

    def write_file(full_path, content = nil, &block)
      content ||= yield(File.read(full_path))

      File.write(full_path, content)
    end

    def replace_string(content, from, to)
      raise "#{from.inspect} not found" unless content.gsub!(from, to)

      content
    end

    def readme_banner(task)
      # rubocop:disable Rails/TimeZone
      <<~BANNER
        # Generated by `rake #{task.name}` on #{Time.now.strftime('%Y-%m-%d')}

        See https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/development/rake_tasks.md#update-openapi-client-for-error-tracking-feature

      BANNER
      # rubocop:enable Rails/TimeZone
    end

    def license
      year = [2022, Date.today.year].uniq.join('-')

      <<~LICENSE
        Copyright #{year} GitLab B.V.

        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      LICENSE
    end
  end
  # :nocov:
end