5 import System
.Reflection
6 import Generator
.Extentions
8 abstract class GeneratorBase
:
9 [Property(Argv
)] _argv
as (string
)
10 [Property(ScriptPath
)] _scriptPath
as string
11 [Property(TemplatesPath
)] _templatesPath
as string
13 [Option('replace', 'r', 'Overwrite existing files')]
14 _overwiteAll
as bool
= false
15 [Option('force', 'f', 'Force overwrite of files that look the same')]
17 [Option('silent', 's', 'Display not output')]
19 [Option('debug', 'd', 'Set debug mode')]
22 _parser
as ArgumentParser
25 _scriptPath
= Path
.Combine(ScriptBasePath
, GeneratorName
)
26 _templatesPath
= "${ScriptPath}/Templates"
27 _parser
= ArgumentParser(self)
29 # This method is called when the generator is created
30 # it can be used to initialize arguments
31 virtual def Init(argv
as (string
)):
32 _argv
= _parser
.SetArguments(argv
)
34 # Main method to run the generator
38 virtual def Usage() as string
:
39 return _parser
.GetUsage()
41 virtual def Help() as string
:
42 return 'No help yet, maybe tomorrow!'
44 #region Template properties
45 GeneratorName
as string
:
48 return n
.Substring(0, n
.Length
- 'generator'.Length
)
50 ScriptBasePath
as string
:
52 return GeneratorFactory
.Instance
.ScriptBasePath
55 #region Template processing methods
56 def Process(template
as string
, tofile
as string
):
57 Process(template
, tofile
, false)
59 def Process(template
as string
, tofile
as string
, keep
as bool
):
60 cleanToFile
= tofile
.ToPath()
61 t
= Template(Path
.Combine(_templatesPath
, template
.ToPath()))
62 result
= t
.Process(CollectVariables())
63 file
= FileInfo(cleanToFile
)
67 Log('exists', cleanToFile
)
71 Log('replace', cleanToFile
)
72 elif file
.Length
== result
.Length
:
73 Log('same', cleanToFile
)
76 if LogAndAskForOverwrite(cleanToFile
):
77 Log('replace', cleanToFile
)
81 Log('create', cleanToFile
)
83 using w
= StreamWriter(cleanToFile
):
86 def Copy(file
as string
, topath
as string
):
87 cleanFile
= Path
.Combine(_templatesPath
, file
.ToPath())
88 cleanToPath
= topath
.ToPath()
89 cleanToFile
= Path
.Combine(cleanToPath
, FileInfo(cleanFile
).Name
)
91 if File
.Exists(cleanToFile
):
92 if _force
or LogAndAskForOverwrite(cleanToFile
):
93 Log('replace', cleanToFile
)
97 Log('create', cleanToFile
)
99 File
.Copy(cleanFile
, cleanToFile
, true)
101 def CopyDir(dir
as string
, topath
as string
):
103 source
= Path
.Combine(_templatesPath
, dir
.ToPath())
104 for file
in Directory
.GetFiles(source
):
105 name
= FileInfo(file
).Name
106 Copy(file
, topath
) if not name
.StartsWith('.')
107 for dir
in Directory
.GetDirectories(source
):
108 name
= DirectoryInfo(dir
).Name
109 CopyDir(dir
, Path
.Combine(topath
, name
)) if not name
.StartsWith('.')
111 def MkDir(path
as string
):
112 cleanPath
= path
.ToPath()
113 if Directory
.Exists(cleanPath
):
114 Log('exists', cleanPath
)
116 Log('create', cleanPath
)
117 Directory
.CreateDirectory(cleanPath
)
120 #region Helper methods
121 virtual def PrintUsage():
122 print "usage: generate ${GeneratorName} ${Usage()}"
124 print _parser
.GetHelp()
128 protected def Log(action
as string
, path
as string
):
130 print action
.PadLeft(10), path
132 protected def Debug(msg
as string
):
133 if _debug
and not _silent
:
136 private def CollectVariables() as Hash
:
138 for p
as PropertyInfo
in GetType().GetProperties():
139 props
.Add(p
.Name
, p
.GetValue(self, null))
142 private def LogAndAskForOverwrite(path
as string
) as bool
:
143 return true if _overwiteAll
145 question
= "${'exists'.PadLeft(10)} ${path} Overwrite? [Ynaq] "
146 answer
= prompt(question
).ToLower()
147 if answer
== 'y' or answer
== '':