vite.py (view raw)
1"""
2Vite - A simple and minimal static site generator.
3"""
4
5import sys
6import argparse
7import pathlib
8import os
9import importlib
10
11parser = argparse.ArgumentParser(description="""
12 A simple and minimal static site generator.
13 """)
14parser.add_argument('action', choices=['new', 'build'])
15# TODO: add help for each action
16parser.add_argument('path', nargs='*')
17
18if len(sys.argv) == 1:
19 parser.print_help()
20 sys.exit(1)
21
22args = parser.parse_args()
23project_path = args.path[0]
24
25
26def create_project(path):
27 try:
28 abs_path = pathlib.Path(path).resolve()
29 cur_path = pathlib.Path('.').resolve()
30 os.makedirs(os.path.join(path, 'build'))
31 os.mkdir(os.path.join(path, 'pages'))
32 os.mkdir(os.path.join(path, 'templates'))
33 create_config(path)
34 os.symlink(os.path.join(cur_path, 'make.py'),
35 os.path.join(abs_path, 'make.py'))
36 print('Created project directory at %s.' % (abs_path))
37 except FileExistsError:
38 print('Error: specified path exists')
39
40
41def create_config(path):
42 with open(path + '/config.py', 'w') as f:
43 f.write("""# config.py - Vite's configuration script
44
45title = ''
46author = ''
47header = ''
48footer = ''
49 """)
50
51
52def build_project(path):
53 try:
54 os.chdir(path)
55 importlib.import_module('make')
56 except FileNotFoundError as e:
57 print('Error: no such file or directory: %s' % (path))
58
59
60def main():
61 if args.action == 'new':
62 create_project(project_path)
63 elif args.action == 'build':
64 build_project(project_path)
65
66
67if __name__ == "__main__":
68 main()