@@ -7,8 +7,8 @@ import argparse
7
7
from clint .textui import prompt , validators , colored , puts
8
8
9
9
## Global Vars
10
- VERSION = "0.1 "
11
- directory = ""
10
+ VERSION = "1.0 "
11
+ directory = os . getcwd ()
12
12
ard_template = "\n \
13
13
#include <Arduino.h>\n \
14
14
#include <Wire.h>\n \
@@ -22,48 +22,100 @@ void loop() {\n\
22
22
"
23
23
24
24
## Command Parser
25
- parser = argparse .ArgumentParser (description = 'Arduino Makefile and boilerplate project generator. For use with Ard-Makefile' )
26
- parser .add_argument ('-v' , '--verbose' , action = 'store_true' , help = "Print file contents during creation" )
27
- parser .add_argument ('-d' , '--directory' , default = '.' , help = 'Directory to run generator' )
25
+ parser = argparse .ArgumentParser (description = 'Arduino Makefile and boilerplate project generator. For use with Ard-Makefile: https://github.com/sudar/Arduino-Makefile. Script created by John Whittington https://github.com/tuna-f1sh 2017\n \n Version: ' + VERSION , usage = '"ardmk-init -b uno -quiet -project" Automonously create boiler plate Arduino Uno project in current working directory with same name. See --help for more.' )
26
+ parser .add_argument ('-v' , '--verbose' , action = 'store_true' , help = "print file contents during creation" )
27
+ parser .add_argument ('-d' , '--directory' , default = directory , help = 'directory to run generator' )
28
+ parser .add_argument ('-b' , '--board' , default = 'uno' , help = 'board tag' )
29
+ parser .add_argument ('-u' , '--micro' , default = 'AUTO' , help = 'microcontroller on board' )
30
+ parser .add_argument ('-f' , '--freq' , default = 'AUTO' , help = 'mlock frequency' )
31
+ parser .add_argument ('-p' , '--port' , default = 'AUTO' , help = 'monitor port' )
32
+ parser .add_argument ('-n' , '--name' , default = os .path .basename (directory ), help = 'project name' )
33
+ parser .add_argument ('-q' , '--quiet' , action = 'store_true' , help = 'run quiet without user prompts' )
34
+ parser .add_argument ('-P' , '--project' , action = 'store_true' , help = 'create boilerplate project with src, lib and bin folder structure' )
35
+ parser .add_argument ('-t' , '--template' , action = 'store_true' , help = 'create bare minimum Arduino source file' )
28
36
args = parser .parse_args ()
29
37
30
38
directory = args .directory
31
39
32
40
def generateMakefile ():
33
- src_dir = ""
34
-
35
- puts (colored .cyan ("Generating Arduino Ard-Makefile project in " + os .path .abspath (directory )))
36
- puts (colored .red ("Any existing Makefile in " + os .path .abspath (directory ) + " will be overwritten!!" ))
37
41
# Header
38
42
fileContents = "# Generated by ard-make version " + VERSION + "\n \n "
39
- btag = prompt .query ('Board tag?' , default = 'uno' )
40
- if not btag == 'uno' :
41
- bsub = prompt .query ('Board sub micro?' , default = 'atmega328' )
42
- f_cpu = prompt .query ('Board frequency' , default = '16000000L' )
43
+
44
+ # Basic
45
+ if not args .quiet :
46
+ puts (colored .cyan ("Generating Arduino Ard-Makefile project in " + os .path .abspath (directory )))
47
+ btag = prompt .query ('Board tag?' , default = 'uno' )
48
+ if not btag == 'uno' :
49
+ bsub = prompt .query ('Board sub micro?' , default = 'atmega328' )
50
+ f_cpu = prompt .query ('Board frequency' , default = '16000000L' )
51
+ else :
52
+ bsub = 'AUTO'
53
+ f_cpu = 'AUTO'
54
+ monitor_port = prompt .query ('Arduino port?' , default = 'AUTO' )
43
55
else :
44
- bsub = 'AUTO'
45
- f_cpu = 'AUTO'
46
- monitor_port = prompt .query ('Arduino port?' , default = 'AUTO' )
56
+ btag = args .board
57
+ bsub = args .micro
58
+ f_cpu = args .freq
59
+ monitor_port = args .port
47
60
48
61
fileContents += checkDefine ('BOARD_TAG' , btag )
49
62
fileContents += checkDefine ('BOARD_SUB' , bsub )
50
63
fileContents += checkDefine ('F_CPU' , f_cpu )
51
64
fileContents += checkDefine ('MONITOR_PORT' , monitor_port )
52
65
53
- if not prompt .yn ('Extended options?' , default = 'n' ):
54
- if not prompt .yn ('Define local folders?' , default = 'n' ):
55
- src_dir = prompt .query ('Sources folder (Makefile will be created here)?' , default = '' , validators = [])
56
- userlibs = prompt .query ('Library folder (will create if does not exist) - AUTO is Sketchbook directory?' , default = 'AUTO' , validators = [])
57
- obj_dir = prompt .query ('Output directory?' , default = 'AUTO' , validators = [])
58
- boards_txt = prompt .query ('Boards file?' , default = 'AUTO' )
59
- isp_prog = prompt .query ('ISP programmer?' , default = 'atmelice_isp' )
60
- isp_port = prompt .query ('ISP port?' , default = 'AUTO' )
61
- if not prompt .yn ('Quiet make?' , default = 'n' ):
62
- fileContents += "ARDUINO_QUIET = 1\n "
63
-
64
- fileContents += checkDefine ('ISP_PROG' , isp_prog )
65
- fileContents += checkDefine ('ISP_PORT' , isp_port )
66
- fileContents += checkDefine ('BOARDS_TXT' , boards_txt )
66
+ # Extended
67
+ if not args .quiet :
68
+ if not prompt .yn ('Extended options?' , default = 'n' ):
69
+ if not prompt .yn ('Define local folders?' , default = 'n' ):
70
+ src_dir = prompt .query ('Sources folder (Makefile will be created here)?' , default = '' , validators = [])
71
+ userlibs = prompt .query ('Library folder (will create if does not exist) - AUTO is Sketchbook directory?' , default = 'AUTO' , validators = [])
72
+ obj_dir = prompt .query ('Output directory?' , default = 'AUTO' , validators = [])
73
+ else :
74
+ src_dir = ''
75
+ userlibs = 'AUTO'
76
+ obj_dir = 'AUTO'
77
+ boards_txt = prompt .query ('Boards file?' , default = 'AUTO' )
78
+ isp_prog = prompt .query ('ISP programmer?' , default = 'atmelice_isp' )
79
+ isp_port = prompt .query ('ISP port?' , default = 'AUTO' )
80
+ if not prompt .yn ('Quiet make?' , default = 'n' ):
81
+ fileContents += "ARDUINO_QUIET = 1\n "
82
+
83
+ fileContents += checkDefine ('ISP_PROG' , isp_prog )
84
+ fileContents += checkDefine ('ISP_PORT' , isp_port )
85
+ fileContents += checkDefine ('BOARDS_TXT' , boards_txt )
86
+
87
+ # Check andd create folders
88
+ checkCreateFolder (src_dir )
89
+ checkCreateFolder (userlibs )
90
+ checkCreateFolder (obj_dir )
91
+
92
+ # Makefile will be in src_dir so lib and bin must be relative
93
+ if src_dir :
94
+ userlibs = "../" + userlibs
95
+ obj_dir = "../" + obj_dir
96
+
97
+ fileContents += checkDefine ('USER_LIB_PATH' , userlibs )
98
+ fileContents += checkDefine ('OBJDIR' , obj_dir )
99
+ else :
100
+ src_dir = ''
101
+
102
+ if args .template or not prompt .yn ('Create template Arduino source?' , default = 'n' ):
103
+ sourceFilename = prompt .query ('Name of project?' , default = os .path .basename (os .getcwd ()))
104
+ if src_dir :
105
+ writeTemplate (src_dir + "/" + sourceFilename )
106
+ else :
107
+ writeTemplate (sourceFilename )
108
+ fileContents += checkDefine ('TARGET' , sourceFilename )
109
+
110
+ else :
111
+ if args .project :
112
+ src_dir = 'src'
113
+ userlibs = 'lib'
114
+ obj_dir = 'bin'
115
+ else :
116
+ src_dir = ''
117
+ userlibs = 'AUTO'
118
+ obj_dir = 'AUTO'
67
119
68
120
# Check andd create folders
69
121
checkCreateFolder (src_dir )
@@ -78,18 +130,19 @@ def generateMakefile():
78
130
fileContents += checkDefine ('USER_LIB_PATH' , userlibs )
79
131
fileContents += checkDefine ('OBJDIR' , obj_dir )
80
132
81
- if not prompt .yn ('Create template Arduino source?' , default = 'n' ):
82
- sourceFilename = prompt .query ('Name of project?' , default = '' )
83
- if src_dir :
84
- writeTemplate (src_dir + "/" + sourceFilename )
85
- else :
86
- writeTemplate (sourceFilename )
87
- fileContents += checkDefine ('TARGET' , sourceFilename )
133
+ if args .project or args .template :
134
+ if src_dir :
135
+ writeTemplate (src_dir + "/" + args .name )
136
+ else :
137
+ writeTemplate (args .name )
138
+ fileContents += checkDefine ('TARGET' , args .name )
88
139
89
140
if not "ARDMK_DIR" in os .environ :
90
- ardmk = prompt .query ('Arduino Makefile path?' , default = '/usr/share/arduino' , validators = [validators .PathValidator ()])
91
- ardmk = "ARDMK_DIR := " + ardmk + "\n "
92
-
141
+ if args .quiet :
142
+ puts (colored .magenta ('Warning: ARDMK_DIR environment variable not defined. Must be defined for Makefile to work' ))
143
+ else :
144
+ ardmk = prompt .query ('Arduino Makefile path?' , default = '/usr/share/arduino' , validators = [validators .PathValidator ()])
145
+ ardmk = "ARDMK_DIR := " + ardmk + "\n "
93
146
94
147
fileContents += "\n include $(ARDMK_DIR)/Arduino.mk"
95
148
@@ -110,8 +163,15 @@ def writeToMakefile(fileContents, path):
110
163
makefile .close ()
111
164
112
165
def writeTemplate (filename ):
113
- src = open ((filename + ".ino" ),'w' )
114
166
puts (colored .cyan ("Writing " + os .path .abspath (filename ) + ".ino..." ))
167
+ if os .path .isfile (filename + '.ino' ):
168
+ if args .quiet :
169
+ puts (colored .red (filename + '.ino' + ' already exists! Stopping.' ))
170
+ return
171
+ puts (colored .red (filename + '.ino' + ' already exists! Overwrite?' ))
172
+ if prompt .yn ('Continue?' , default = 'n' ):
173
+ return
174
+ src = open ((filename + ".ino" ),'w' )
115
175
if args .verbose :
116
176
puts (colored .yellow (ard_template ))
117
177
src .write ("/* Project: " + filename + " */\n " + ard_template )
@@ -134,6 +194,13 @@ def main():
134
194
checkCreateFolder (directory )
135
195
# Change to dir so all commands are run relative
136
196
os .chdir (directory )
197
+ if os .path .isfile ('Makefile' ):
198
+ if args .quiet :
199
+ puts (colored .red ('Makefile in ' + os .path .abspath (directory ) + ' already exists! Stopping.' ))
200
+ return
201
+ puts (colored .red ('Makefile in ' + os .path .abspath (directory ) + ' already exists! Overwrite?' ))
202
+ if prompt .yn ('Continue?' , default = 'n' ):
203
+ return
137
204
generateMakefile ();
138
205
139
206
main ()
0 commit comments