canoe 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6650afffc9e3aa13bb3489b419cd7e4f2b44f1f104be6fdffa77d0b2a66d072
4
- data.tar.gz: fef3df6c97065e9274c2f95e2b2993a740d46a563e9e04b7359fbb4b69d4f95d
3
+ metadata.gz: 7b97a389b6d90008ad3e69fc0937e7382f904574ae28180f2fe98c9920f41aca
4
+ data.tar.gz: 8e1fe69ac5b71343a68389d2053a691caa4df6b7ca51c03c2bc50ec5e3527d51
5
5
  SHA512:
6
- metadata.gz: 1c093606ffcc64c3679afc3b8998841e7e1b8e83a2315d5bc8f1ad91e2c3b36d3eac5d649dda55d27287600ba52c9880c2f65b612b65f5fb57dbeac06ddc865b
7
- data.tar.gz: 7483c43fa9d8f91c27be1b042f2b151187a867e85fd4cffc57f84b0a01c892583ef3f7d57ad6c816ad50668cabdfcb4e6c955db9c95aba8037ccf30c02eb0054
6
+ metadata.gz: 7d8698fea60f94b97246aecd1af2b51a2996871fdabc2aa0fab47c84dd764ac20c7b460cd3183d653777f11b8c403c36b2f6054f8d60baff65ca7e47f11fac9c
7
+ data.tar.gz: c740ad122d12eb555aae933c931cfab5dd2225057c4367cfae7f8abd1811601cafbf1e9a3987fdf0992a71d849c4d1f2d30d85bb6b078e6cb2a574e9a0ea336a
data/bin/canoe CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'canoe'
3
3
 
4
- Canoe.new.parse ARGV
4
+ Canoe::Builder.new.parse ARGV
data/lib/canoe.rb CHANGED
@@ -1,23 +1,27 @@
1
- require_relative "workspace"
2
- require_relative "cmd"
3
- require_relative "source_files"
1
+ require_relative 'workspace/workspace'
2
+ require_relative 'cmd'
3
+ require_relative 'source_files'
4
4
 
5
- class Canoe
6
- def initialize
7
- options = ['new',
8
- 'add',
9
- 'build',
10
- 'generate',
11
- 'run',
12
- 'dep',
13
- 'clean',
14
- 'version',
15
- 'help',
16
- 'update']
17
- @cmd = CmdParser.new options
18
- end
5
+ module Canoe
6
+ class Builder
7
+ def initialize
8
+ options = ['new',
9
+ 'add',
10
+ 'build',
11
+ 'generate',
12
+ 'make',
13
+ 'run',
14
+ 'dep',
15
+ 'clean',
16
+ 'version',
17
+ 'help',
18
+ 'update',
19
+ 'test']
20
+ @cmd = CmdParser.new options
21
+ end
19
22
 
20
- def parse(args)
23
+ def parse(args)
21
24
  @cmd.parse args
25
+ end
22
26
  end
23
27
  end
data/lib/cmd.rb CHANGED
@@ -1,114 +1,99 @@
1
- require_relative "workspace"
2
- require_relative "err"
3
- require_relative "config_reader"
1
+ require_relative 'workspace/workspace'
2
+ require_relative 'util'
3
+ require_relative 'config_reader'
4
4
 
5
5
  ##
6
6
  # class CmdParser
7
7
  # Parsing command arguments passed to canoe
8
- class CmdParser
9
- include Err
10
- def initialize(options)
11
- @options = options
12
- end
8
+ module Canoe
9
+ class CmdParser
10
+ include Err
11
+ include WorkSpaceUtil
13
12
 
14
- def parse(args)
15
- if args.size < 1
16
- abort_on_err "please give one command among #{@options.join(', ')}"
13
+ def initialize(options)
14
+ @options = options
17
15
  end
18
16
 
19
- unless @options.include?(args[0])
20
- abort_on_err "unknown command #{args[0]}"
17
+ def parse(args)
18
+ if args.size < 1
19
+ abort_on_err "please give one command among #{@options.join(", ")}"
20
+ end
21
+
22
+ unless @options.include?(args[0])
23
+ abort_on_err "unknown command #{args[0]}"
24
+ end
25
+
26
+ self.send "parse_#{args[0]}", args[1..]
21
27
  end
22
28
 
23
- self.send "parse_#{args[0]}", args[1..]
24
- end
29
+ private
30
+ def parse_new(args)
31
+ abort_on_err "not enough arguments to canoe new" if args.size < 1
32
+
33
+ name, mode = nil, "bin"
34
+ suffixes = ["cpp", "hpp"]
35
+
36
+ args.each do |arg|
37
+ case arg
38
+ when "--bin", "--lib"
39
+ mode = arg[2..]
40
+ when /--suffix=(\w+)\:(\w+)/
41
+ suffixes[0], suffixes[1] = $1, $2
42
+ else
43
+ name = arg unless name
44
+ end
45
+ end
25
46
 
26
- private
27
- def get_current_workspace
28
- abort_on_err "not in a canoe workspace" unless File.exists? ".canoe"
29
- config = ConfigReader.extract_flags("config.json")
47
+ abort_on_err('please give a name to this project') unless name
48
+ WorkSpace.new(name, mode.to_sym, suffixes[0], suffixes[1], true).new
49
+ end
30
50
 
31
- src_sfx = config["source-suffix"] ? config["source-suffix"] : "cpp"
32
- hdr_sfx = config["header-suffix"] ? config["header-suffix"] : "hpp"
51
+ def parse_add(args)
52
+ if args.size < 1
53
+ abort_on_err "it's not reasonable to add a component with no name given"
54
+ end
33
55
 
34
- name = Dir.pwd.split("/")[-1]
35
- mode = File.exists?("src/main.#{src_sfx}") ? :bin : :lib
56
+ get_current_workspace.add args
57
+ end
36
58
 
37
- Dir.chdir('..') do
38
- return WorkSpace.new(name, mode, src_sfx, hdr_sfx)
59
+ def parse_build(args)
60
+ get_current_workspace.build args
39
61
  end
40
- end
41
62
 
42
- def parse_new(args)
43
- abort_on_err "not enough arguments to canoe new" if args.size < 1
44
-
45
- name, mode = nil, "bin"
46
- suffixes = ["cpp", "hpp"]
47
-
48
- args.each do |arg|
49
- case arg
50
- when '--bin', '--lib'
51
- mode = arg[2..]
52
- when /--suffix=(\w+)\:(\w+)/
53
- suffixes[0], suffixes[1] = $1, $2
54
- else
55
- name = arg unless name
56
- end
63
+ def parse_generate(args)
64
+ get_current_workspace.generate
57
65
  end
58
-
59
- abort_on_err("please give a name to this project") unless name
60
- WorkSpace.new(name, mode.to_sym, suffixes[0], suffixes[1]).new
61
- end
62
66
 
63
- def parse_add(args)
64
- if args.size < 1
65
- abort_on_err "it's not reasonable to add a component with no name given"
67
+ def parse_run(args)
68
+ get_current_workspace.run args
66
69
  end
67
70
 
68
- get_current_workspace.add args
69
- end
70
-
71
- def parse_build(args)
72
- get_current_workspace.build args
73
- end
71
+ def parse_dep(args)
72
+ get_current_workspace.dep
73
+ end
74
74
 
75
- def parse_generate(args)
76
- get_current_workspace.generate
77
- end
78
-
79
- def parse_run(args)
80
- get_current_workspace.run args
81
- end
75
+ def parse_clean(args)
76
+ get_current_workspace.clean args
77
+ end
82
78
 
83
- def parse_dep(args)
84
- get_current_workspace.dep
85
- end
86
-
87
- def parse_clean(args)
88
- get_current_workspace.clean
89
- end
79
+ def parse_test(args)
80
+ get_current_workspace.test args
81
+ end
90
82
 
91
- def parse_test(args)
92
- get_current_workspace.test args
93
- end
83
+ def parse_version(args)
84
+ WorkSpace.version
85
+ end
94
86
 
95
- def parse_version(args)
96
- puts <<~VER
97
- canoe v0.3.0
98
- For features in this version, please visit https://github.com/Dicridon/canoe
99
- Currently, canoe can do below:
100
- - project creation
101
- - project auto build and run (works like Cargo for Rust)
102
- - project structure management
103
- by XIONG Ziwei
104
- VER
105
- end
106
-
107
- def parse_help(args)
108
- WorkSpace.help
109
- end
87
+ def parse_help(args)
88
+ WorkSpace.help
89
+ end
110
90
 
111
- def parse_update(args)
112
- get_current_workspace.update
91
+ def parse_update(args)
92
+ get_current_workspace.update
93
+ end
94
+
95
+ def parse_make(args)
96
+ get_current_workspace.make
97
+ end
113
98
  end
114
99
  end
data/lib/coloring.rb ADDED
@@ -0,0 +1,23 @@
1
+ ##
2
+ # gem Colorize is a great tool, but I don't want add dependencies to Canoe
3
+ class String
4
+ def self.define_coloring_methods
5
+ colors = {
6
+ 30 => :black,
7
+ 31 => :red,
8
+ 32 => :green,
9
+ 33 => :yellow,
10
+ 34 => :blue,
11
+ 35 => :magenta,
12
+ 36 => :cyan,
13
+ 37 => :white,
14
+ }
15
+ colors.each do |k, v|
16
+ define_method v do
17
+ "\033[#{k}m#{self}\033[0m"
18
+ end
19
+ end
20
+ end
21
+
22
+ define_coloring_methods
23
+ end
data/lib/compiler.rb CHANGED
@@ -1,51 +1,49 @@
1
+ require_relative 'util'
2
+
1
3
  ##
2
4
  # class Compiler
3
5
  # Storing compiler name in String and flags as an array
4
- class Compiler
5
- attr_reader :name, :flags
6
- ##
7
- # @name: String
8
- # @flgs: Array of String
9
- def initialize(name, flgs)
10
- @name = name
11
- @linking_flags = flgs.filter {|f| f.start_with? "-l"}
12
- @compiling_flags = flgs - @linking_flags
13
- end
14
-
15
- def compiling_flags_as_str
16
- @compiling_flags.join " "
17
- end
18
-
19
- def linking_flags_as_str
20
- @linking_flags.join " "
21
- end
22
-
23
-
24
- def append_compiling_flag(flag)
25
- @compiling_flags << flag
26
- end
27
-
28
- def append_linking_flag(flag)
29
- @linking_flags << flag
30
- end
31
-
32
- def compile(src, out)
33
- puts "#{name} -o #{out} #{compiling_flags_as_str} -c #{src}"
34
- system "#{name} -o #{out} #{compiling_flags_as_str} -c #{src}"
35
- end
36
-
37
- def link_executable(out, objs)
38
- puts "#{name} -o #{out} #{objs.join(" ")} #{linking_flags_as_str}"
39
- system "#{name} -o #{out} #{objs.join(" ")} #{linking_flags_as_str}"
40
- end
41
-
42
- def link_shared(out, objs)
43
- puts "#{name} -shared -o #{out}.so #{objs.join(" ")} #{linking_flags_as_str}"
44
- system "#{name} -shared -o #{out}.so #{objs.join(" ")} #{linking_flags_as_str}"
45
- end
46
-
47
- def inspect
48
- puts "compiler name: #{name.inspect}"
49
- puts "compiler flags: #{flags.inspect}"
6
+ module Canoe
7
+ class Compiler
8
+ include SystemCommand
9
+
10
+ attr_reader :name, :flags
11
+
12
+ ##
13
+ # @name: String
14
+ # @flgs: Array of String
15
+ def initialize(name, compiling_flags, linking_flags)
16
+ @name = name
17
+ @linking_flags = linking_flags
18
+ @compiling_flags = compiling_flags
19
+ end
20
+
21
+ def compiling_flags_as_str
22
+ @compiling_flags.join ' '
23
+ end
24
+
25
+ def linking_flags_as_str
26
+ @linking_flags.join ' '
27
+ end
28
+
29
+ def append_compiling_flag(flag)
30
+ @compiling_flags << flag
31
+ end
32
+
33
+ def append_linking_flag(flag)
34
+ @linking_flags << flag
35
+ end
36
+
37
+ def compile(src, out)
38
+ issue_command "#{name} -o #{out} #{compiling_flags_as_str} -c #{src}"
39
+ end
40
+
41
+ def link_executable(out, objs)
42
+ issue_command "#{name} -o #{out} #{objs.join(' ')} #{linking_flags_as_str}"
43
+ end
44
+
45
+ def link_shared(out, objs)
46
+ issue_command "#{name} -shared -o #{out}.so #{objs.join(' ')} #{linking_flags_as_str}"
47
+ end
50
48
  end
51
49
  end
data/lib/config_reader.rb CHANGED
@@ -5,7 +5,7 @@ require 'json'
5
5
  # Just read a json file
6
6
  class ConfigReader
7
7
  def self.extract_flags(file)
8
- abort_on_err("config file #{file} does not exsit") unless File.exists? file
8
+ abort_on_err("config file #{file} does not exsit") unless File.exist? file
9
9
  JSON.parse(File.read(file))
10
10
  end
11
11
  end
data/lib/default_files.rb CHANGED
@@ -5,30 +5,35 @@
5
5
  class DefaultFiles
6
6
  class << self
7
7
  def open_file_and_write(filename, content)
8
- File.open(filename, "w") {|f|
8
+ File.open(filename, 'w') do |f|
9
9
  f.write(content)
10
- }
10
+ end
11
11
  end
12
12
 
13
- def create_config(path, src_sfx='cpp', hdr_sfx='hpp')
13
+ def create_config(path, src_sfx = 'cpp', hdr_sfx = 'hpp')
14
14
  open_file_and_write(
15
- "#{path}/config.json",
15
+ "#{path}/config.json",
16
16
  <<~CONFIG
17
17
  {
18
18
  "compiler": "clang++",
19
19
  "header-suffix": "#{hdr_sfx}",
20
20
  "source-suffix": "#{src_sfx}",
21
21
  "flags": {
22
- "opt": "-O2",
23
- "debug": "-g",
24
- "std": "-std=c++17"
22
+ "compile": {
23
+ "opt": "-O2",
24
+ "debug": "-g",
25
+ "std": "-std=c++17"
26
+ },
27
+ "link": {
28
+
29
+ }
25
30
  }
26
31
  }
27
32
  CONFIG
28
33
  )
29
34
  end
30
35
 
31
- def create_main(path, suffix='cpp')
36
+ def create_main(path, suffix = 'cpp')
32
37
  open_file_and_write(
33
38
  "#{path}/main.#{suffix}",
34
39
  <<~DOC
@@ -40,46 +45,34 @@ class DefaultFiles
40
45
  )
41
46
  end
42
47
 
43
- def create_lib_header(path, lib_name, suffix='hpp')
48
+ def create_lib_header(path, lib_name, suffix = 'hpp')
44
49
  open_file_and_write(
45
50
  "#{path}/#{lib_name}.#{suffix}",
46
51
  <<~DOC
47
52
  #ifndef __#{lib_name.upcase}__
48
53
  #define __#{lib_name.upcase}__
49
-
54
+
50
55
  #endif
51
56
  DOC
52
57
  )
53
58
  end
54
59
 
55
- # def create_emacs_dir_local(path)
56
- # open_file_and_write(
57
- # "#{path}/.dir-locals.el",
58
- # <<~DOC
59
- # ((nil . ((company-clang-arguments . ("-I./src/components/"
60
- # "-I./components/"))))
61
- # (nil . ((company-c-headers-path-user . ("./src/components/"
62
- # "./components/")))))
63
- # DOC
64
- # )
65
- # end
66
-
67
- def create_cpp(filename, src_sfx='cpp', hdr_sfx='hpp')
60
+ def create_cpp(filename, src_sfx = 'cpp', hdr_sfx = 'hpp')
68
61
  open_file_and_write(
69
- "#{filename}.#{src_sfx}",
62
+ "#{filename}.#{src_sfx}",
70
63
  <<~DOC
71
64
  #include "#{filename}.#{hdr_sfx}"
72
65
  DOC
73
66
  )
74
67
  end
75
68
 
76
- def create_hpp(workspace, prefix, filename, hdr_sfx='hpp')
69
+ def create_hpp(workspace, prefix, filename, hdr_sfx = 'hpp')
77
70
  open_file_and_write(
78
71
  "#{filename}.#{hdr_sfx}",
79
72
  <<~DOC
80
73
  #ifndef __#{workspace.upcase}__#{prefix.upcase}__#{filename.upcase}__
81
74
  #define __#{workspace.upcase}__#{prefix.upcase}__#{filename.upcase}__
82
-
75
+
83
76
  #endif
84
77
  DOC
85
78
  )