Top Level Namespace

Defined Under Namespace

Modules: Alet, IRBUtils Classes: Apex, Conn, Deploy, Describe, Export, GenerateModel, GenerateResource, Grep, Org, Query, Shell

Instance Method Summary collapse

Instance Method Details

#gen_apex(argv, params) ⇒ Object



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
# File 'lib/alet/irb/command/gen/apex.rb', line 1

def gen_apex(argv, params)
  return if argv.empty?

  name = argv.first

  base_dir = Dir.pwd
  dx_dir = 'force-app/main/default'
  dir = if FileTest.exist?(%|#{base_dir}/#{dx_dir}|)
          %|#{base_dir}/#{dx_dir}/#{params[:trigger] ? 'triggers' : 'classes'}|
        else
          base_dir
        end

  if FileTest.exist?(%|#{dir}/#{name}#{params[:trigger] ? '.trigger' : '.cls'}|)
    pastel = Pastel.new
    puts pastel.red(t('gen.error.file_duplicated'))
    return
  end

  if params[:trigger]
    event_map = {
      'bi' => 'before insert',
      'bu' => 'before update',
      'bd' => 'before delete',
      'ai' => 'after insert',
      'au' => 'after update',
      'ad' => 'after delete',
      'aud' => 'after undelete',
    }
    events = params[:event]&.split(',')&.map{|e| event_map[e]}&.compact
    sf.apex.generate_trigger name, output_dir: dir, sobject: params[:sobject], event: events
  else
    sf.apex.generate_class name, output_dir: dir
    sf.apex.generate_class %|#{name}Test|, output_dir: dir, template: :ApexUnitTest
  end
end

#gen_lwc(argv, params) ⇒ Object



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
# File 'lib/alet/irb/command/gen/lwc.rb', line 3

def gen_lwc(argv, params)
  target_map = {
    'app' => 'lightning__AppPage',
    'flow' => 'lightning__FlowScreen',
    'home' => 'lightning__HomePage',
    'action' => 'lightning__RecordAction',
    'record' => 'lightning__RecordPage',
    'tab' => 'lightning__Tab',
    'bar' => 'lightning__UtilityBar',
  }

  return if argv.empty?

  pastel   = Pastel.new
  name     = argv.first
  name[0]  = name[0].downcase
  base_dir = Dir.pwd
  dx_dir   = 'force-app/main/default'
  dir = if FileTest.exist?(%|#{base_dir}/#{dx_dir}|)
          %|#{base_dir}/#{dx_dir}/lwc|
        else
          base_dir
        end
  metafile = %|#{dir}/#{name}/#{name}.js-meta.xml|
  meta_changed = false

  if FileTest.exist?(%|#{dir}/#{name}|)
    puts pastel.red(t('gen.error.file_duplicated'))
    return
  end

  sf.lightning.generate_component name, type: :lwc, output_dir: dir, api_version: Alet.config.org.api_version

  doc = Nokogiri::XML(File.open(metafile), nil, 'UTF-8', Nokogiri::XML::ParseOptions::NOBLANKS)

  if params[:label]
    node = doc.at_css('LightningComponentBundle')
    label = Nokogiri::XML::Node.new("masterLabel", node)
    label.content = params[:label]
    node.add_child label
    meta_changed = true
  end

  if params[:description]
    node = doc.at_css('LightningComponentBundle')
    desc = Nokogiri::XML::Node.new("description", node)
    desc.content = params[:description]
    node.add_child desc
    meta_changed = true
  end

  if params[:exposed]
    node = doc.at_css('isExposed')
    node.content = true
    meta_changed = true
  end

  if params[:target]
    node = doc.at_css('LightningComponentBundle')
    targets_node = Nokogiri::XML::Node.new("targets", node)

    targets = params[:target].split(',').map{|t| target_map[t]}
    targets.each do |target|
      target_node = Nokogiri::XML::Node.new("target", targets_node){|n| n.content = target}
      targets_node.add_child target_node 
    end
    node.add_child targets_node

    if targets.include?('lightning__RecordAction') || targets.include?('lightning__RecordPage')
      target_configs = Nokogiri::XML::Node.new('targetConfigs', node)
      action_config  = Nokogiri::XML::Node.new('targetConfig', target_configs){|n| n.set_attribute('targets', 'lightning__RecordAction') }
      record_config  = Nokogiri::XML::Node.new('targetConfig', target_configs){|n| n.set_attribute('targets', 'lightning__RecordPage') }

      if targets.include?('lightning__RecordAction')
        action_type = Nokogiri::XML::Node.new('actionType', action_config)
        action_type.content = 'ScreenAction'
        action_config.add_child action_type
        target_configs.add_child action_config
      end

      if targets.include?('lightning__RecordAction') && params[:object]
        objects = Nokogiri::XML::Node.new('objects', record_config)
        params[:object].split(',').each do |object_name|
          object = Nokogiri::XML::Node.new('object', objects)
          object.content = object_name
          objects.add_child object
        end
        record_config.add_child objects
        target_configs.add_child record_config
      end
      node.add_child target_configs
    end
    meta_changed = true
  end

  File.open(metafile, 'w') {|f| f.write(doc.to_xml indent: 4)} if meta_changed
end

#reset_connectionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alet/irb/shared_functions.rb', line 30

def reset_connection
  org = sf.org.display target_org: Alet.config.cli_options[:"target-org"]

  unless org.connected?
    sf.org. target_org: org.alias, instance_url: org.instance_url
    org = sf.org.display target_org: Alet.config.cli_options[:"target-org"]
  end

  Alet.config.org = org

  reset_rest_client
  reset_sobject_model_adapter
end

#reset_rest_clientObject



44
45
46
47
48
49
50
51
# File 'lib/alet/irb/shared_functions.rb', line 44

def reset_rest_client
  rest_client = SObjectModel::Rest::Client.new(
                  instance_url: Alet.config.org.instance_url,
                  access_token: Alet.config.org.access_token,
                  api_version: Alet.config.org.api_version)

  Alet.rest_client = rest_client
end

#reset_sobject_model_adapterObject



53
54
55
56
# File 'lib/alet/irb/shared_functions.rb', line 53

def reset_sobject_model_adapter
  adapter = SObjectModel::Adapter::Rest.new(Alet.rest_client)
  SObjectModel.connection = adapter
end

#show_org_settingsObject



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/alet/irb/shared_functions.rb', line 1

def show_org_settings
  info = sf.org.display target_org: Alet.config.org.alias

  table =
    TTY::Table.new(rows: [
      [:id, info.id],
      [:alias, info.alias],
      [:user_name, info.user_name],
      [:status, info.status],
      [:instance_url, info.instance_url],
      [:api_version, info.api_version],
      [:access_token, info.access_token]
    ])
  puts table.render :unicode
rescue => e
  pastel = Pastel.new
  puts pastel.red(e.message.sub("See more help with --help", ''))
end

#show_rest_client_settingsObject



20
21
22
23
24
25
26
27
28
# File 'lib/alet/irb/shared_functions.rb', line 20

def show_rest_client_settings
  table =
    TTY::Table.new(rows: [
      [:instance_url, Alet.rest_client.instance_url],
      [:access_token, Alet.rest_client.access_token],
      [:api_version, Alet.rest_client.api_version],
    ])
  puts table.render :unicode
end

#t(indicator) ⇒ Object



16
17
18
# File 'lib/alet/app.rb', line 16

def t(indicator)
  I18n.t(indicator)
end