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

snowplow_event_definition_generator.rb « gitlab « generators « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 497d0cd512ae89017be6ecd9a732be90f0f7c074 (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
# frozen_string_literal: true

require 'rails/generators'

module Gitlab
  class SnowplowEventDefinitionGenerator < Rails::Generators::Base
    CE_DIR = 'config/events'
    EE_DIR = 'ee/config/events'

    source_root File.expand_path('../../../generator_templates/snowplow_event_definition', __dir__)

    desc 'Generates an event definition yml file'

    class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if event is for ee'
    class_option :category, type: :string, optional: false, desc: 'Category of the event'
    class_option :action, type: :string, optional: false, desc: 'Action of the event'
    class_option :force, type: :boolean, optional: true, default: false, desc: 'Overwrite existing definition'

    def create_event_file
      raise "Event definition already exists at #{file_path}" if definition_exists? && !force_definition_override?

      template "event_definition.yml", file_path, force: force_definition_override?
    end

    def distributions
      (ee? ? ['- ee'] : ['- ce', '- ee']).join("\n")
    end

    def event_category
      options[:category]
    end

    def event_action
      options[:action]
    end

    def milestone
      Gitlab::VERSION.match('(\d+\.\d+)').captures.first
    end

    def ee?
      options[:ee]
    end

    def force_definition_override?
      options[:force]
    end

    private

    def definition_exists?
      File.exist?(ce_file_path) || File.exist?(ee_file_path)
    end

    def file_path
      ee? ? ee_file_path : ce_file_path
    end

    def ce_file_path
      File.join(CE_DIR, file_name)
    end

    def ee_file_path
      File.join(EE_DIR, file_name)
    end

    def file_name
      "#{event_category}_#{event_action}.yml".underscore.gsub("/", "__")
    end
  end
end