Class: ManageYaml::MkSemiLattice

Inherits:
Object
  • Object
show all
Defined in:
lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MkSemiLattice

Returns a new instance of MkSemiLattice.



5
6
7
8
9
10
11
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 5

def initialize(options)
  @parent_dir = options[:parent_dir]
  @semi_dir = options[:semi_dir]
  @semi_lattice_yaml_path = options[:semi_lattice_yaml_path]
  @options = options
  p @options
end

Class Method Details

.at_exit_action(app, semi_dir, parent_dir) ⇒ Object

アプリ終了時の状態保存



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
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 69

def self.at_exit_action(app, semi_dir, parent_dir)
  nodes_data = app.nodes.map do |n|
    #p [n.label, n.fixed, n.color]
    {
      id: app.node_table.key(n),
      name: n.name,
      type: n.type,
      file_path: n.file_path,
      icon_path: n.icon_path,
      x: n.x,
      y: n.y,
      color: n.color,
      fixed: n.fixed
    }
  end
  edges_data = app.edges.map do |e|
    {
      from: app.node_table.key(e.from),
      to: app.node_table.key(e.to)
    }
  end

  yaml_data = { nodes: nodes_data, edges: edges_data }
  yaml_text = MkNodeEdge.add_edge_comments(yaml_data)
  if Dir.exist?(semi_dir)
    File.write(File.join(semi_dir, "semi_lattice.yaml"), yaml_text)
    puts "Semi-lattice state saved to #{File.join(semi_dir, "semi_lattice.yaml")}"
  else
    File.write(File.join('.', "semi_lattice.yaml"), yaml_text)
    puts "Semi-lattice state saved to #{File.join('.', "semi_lattice.yaml")}"
  end
  InitEnv::Log.event("exited", parent_dir: parent_dir)
end

Instance Method Details

#prepare_paths_and_flagsObject

新メソッド: 初期ファイル・ステップ・入力パス・フラグをまとめて取得



60
61
62
63
64
65
66
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 60

def prepare_paths_and_flags
  init_file, init_step = select_init_file_and_step
  p ["init_file", init_file, init_step]
  input_path, with_semi_lattice_yaml = select_input_path_and_flag(init_file, init_step)
  p ["input_path", input_path, with_semi_lattice_yaml]
  [input_path, with_semi_lattice_yaml]
end

#select_init_file_and_stepObject

ARGVとoptionsから初期ファイル・初期ステップを決定



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 14

def select_init_file_and_step
  p ['ARGV', ARGV]
  if (ARGV[0] == '.' || ARGV[0].nil?) && !@options[:file]
    if File.exist?(@semi_lattice_yaml_path)
      [@semi_lattice_yaml_path, :from_semi_lattice]
    else
      ['.', :from_dir]
    end
  else
    [ARGV[0], @options[:init_step]]
  end
end

#select_input_path_and_flag(init_file, init_step) ⇒ Object

初期ステップに応じて入力ファイルとwith_semi_lattice_yamlを決定



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
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 28

def select_input_path_and_flag(init_file, init_step)
  case init_step
  when :from_dir
    Dir.mkdir(@semi_dir) unless Dir.exist?(@semi_dir)
    in_path = init_file
    out_path = File.join(@semi_dir, 'dir_tree.yaml')
    MkDirYaml.new(path: in_path, layer: @options[:layer], output_file: out_path, options: @options)
    in_path = out_path
    out_path = File.join(@semi_dir, 'dir_node_edge.yaml')
    MkNodeEdge.new(input_path: in_path, output_path: out_path)
    [out_path, false]
  when :from_tree
    init_file = @options[:file]
    base = File.basename(init_file, File.extname(init_file))
    in_path = init_file
    out_path = File.join(@parent_dir, "#{base}_node_edge.yaml")
    MkNodeEdge.new(input_path: in_path, output_path: out_path)
    [out_path, false]
  when :from_node_edge
    if File.exist?(File.join(@parent_dir, 'semi_lattice.yaml'))
      puts "Warning: semi_lattice.yaml already exists in current directory.".yellow
      exit 1
    end
    [@options[:file], false]
  when :from_semi_lattice
    [init_file, true]
  else
    raise "Unknown init_step: #{init_step}"
  end
end