-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsharp-netcore-skeleton.py
More file actions
236 lines (187 loc) · 9.1 KB
/
fsharp-netcore-skeleton.py
File metadata and controls
236 lines (187 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
58
59
60
61
62
63
64
65
66
67
68
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""Netcore Skeleton. Create an F# netcore project in a directory with project name.
Usage:
fsharp-netcore-skeleton <project_name_or_path> (--console [--watcher] | --classlib)
[--force] [--nogit] [--single-project] [--no-build]
Options:
--console make an executable type project.
--classlib make a library type project.
--watcher add a dotnet watcher to run the test and executable on code change. Console applications only.
--force try to run this skeleton script even though the project directory already exists. May fail horribly.
--nogit do not git init and git add
--single-project one project only (no test project), no solution
--no-build do not include a build script (do everything manually with dotnet/paket commands). Implies --single-project.
"""
from docopt import docopt
import os
from os.path import join
import shutil
from subprocess import check_output
import sys
import xml.dom.minidom as minidom
arguments = docopt(__doc__)
# Inputs
project_name_or_path = arguments['<project_name_or_path>']
if "/" in project_name_or_path or os.path.sep in project_name_or_path:
project_name = os.path.split(project_name_or_path)[-1]
is_project_path = True
else:
project_name = project_name_or_path
is_project_path = False
is_classlib = arguments['--classlib']
watcher = arguments['--watcher']
run_git = not arguments['--nogit']
single_project = arguments['--single-project']
no_build = arguments['--no-build']
single_project = single_project or no_build
require_test_project = not single_project
test_project_name = project_name + "Test"
pwd = os.getcwd()
project_dir = join(pwd, project_name) if not is_project_path else project_name_or_path
dir_already_existed = os.path.exists(project_dir)
script_dir = os.path.dirname(os.path.realpath(__file__))
src_fsproj = "{}.fsproj".format(project_name)
test_fsproj = "{}.fsproj".format(test_project_name)
if dir_already_existed and not arguments['--force']:
print("Project directory already exists - remove and try again or use --force")
sys.exit(1)
# OS Utils
def mkdir(directory):
try:
os.mkdir(directory)
except OSError:
pass
def touch(file_path):
open(file_path, 'a').close()
def nuke_tree(directory):
if sys.platform.startswith("win"):
print(check_output(["rmdir /S /Q", directory], shell=True))
else:
shutil.rmtree(directory)
# Project setup
def copy_file_to_project(template_file_name, new_project_relative_path):
shutil.copyfile(join(script_dir, "resources", template_file_name),
join(project_dir, new_project_relative_path))
def touch_project_file(file_name):
touch(join(project_dir, file_name))
def update_fsproj_target_framework(fsproj_path, new_version="netcoreapp2.0"):
fsproj_xml = open(fsproj_path).read()
fsproj_xml = fsproj_xml.replace("netcoreapp1.1", new_version) # works for now
open(fsproj_path, "w").write(fsproj_xml)
def run_cmd(cmd):
output = str(check_output(cmd, shell=True), 'utf-8')
print(output)
return output
def add_src_fsproj_package_reference(package_name):
run_cmd("dotnet add src/{} package {}".format(src_fsproj, package_name))
def add_test_fsproj_package_reference(package_name):
run_cmd("dotnet add test/{} package {}".format(test_fsproj, package_name))
def add_test_run_msbuild_watch_target():
# If a classlib then the fake serverTests target just runs the tests and exits the process
# (Ionide) already has a way to watch the source tree for changes and re-run the tests with Expecto
# However, if a console application we may want to have a watch process that runs the tests and re-runs
# the application - if it is a server process for example.
fsproj_dom = minidom.parse("test/{}".format(test_fsproj))
project_node = fsproj_dom.firstChild
assert project_node.nodeName == 'Project'
target = fsproj_dom.createElement("Target")
target.setAttribute("Name", "TestAndRun")
execRunServerTest = fsproj_dom.createElement("Exec")
execRunServerTest.setAttribute("Command", "dotnet run")
execRunServerTest.setAttribute("WorkingDirectory", "./")
target.appendChild(execRunServerTest)
if watcher:
execRunServerExecutable = fsproj_dom.createElement("Exec")
execRunServerExecutable.setAttribute("Command", "dotnet run")
execRunServerExecutable.setAttribute("WorkingDirectory", "../src/")
target.appendChild(execRunServerExecutable)
project_node.appendChild(target)
new_dom_text = fsproj_dom.toprettyxml()
open("test/{}".format(test_fsproj), "w").write(new_dom_text)
if not watcher:
# Remove RunWatch action
build_fsx = open("build.fsx").read()
build_fsx = build_fsx.replace('"RunServerTests" ==> "RunWatch"', "")
open("build.fsx", "w").write(build_fsx)
def patch_expecto_template():
# Deal with `dotnet restore` failing with wildcards in version numbers (presumably a bug)
fsproj_dom = minidom.parse("test/{}".format(test_fsproj))
project_node = fsproj_dom.firstChild
refs = project_node.getElementsByTagName("PackageReference")
for package_ref in (r for r in refs if r.hasAttribute("Include")):
package_name = package_ref.getAttribute("Include")
if "Expecto" == package_name:
package_ref.setAttribute("Version", "5.0.1")
break
# Update dotnet watcher version
cli_refs = project_node.getElementsByTagName("DotNetCliToolReference")
for cli_ref in (cr for cr in cli_refs if cr.hasAttribute("Include")):
tool_name = cli_ref.getAttribute("Include")
if "Microsoft.DotNet.Watcher.Tools" == tool_name:
cli_ref.setAttribute("Version", "2.0.0")
new_dom_text = fsproj_dom.toprettyxml()
open("test/{}".format(test_fsproj), "w").write(new_dom_text)
def make_project():
mkdir(project_dir)
os.chdir(project_dir)
project_template = "classlib" if is_classlib else "console"
run_cmd('dotnet new {} --language "F#" --output src --name {}'.format(project_template, project_name))
if require_test_project:
if "expecto" not in run_cmd("dotnet new -l"):
run_cmd("dotnet new -i 'Expecto.Template::*'")
run_cmd("dotnet new expecto --output test --name {}".format(test_project_name))
run_cmd("dotnet new sln")
run_cmd("dotnet sln add src/" + src_fsproj)
run_cmd("dotnet sln add test/" + test_fsproj)
update_fsproj_target_framework(join("test", test_fsproj))
run_cmd("dotnet add test/{} reference src/{}".format(test_fsproj, src_fsproj))
patch_expecto_template()
mkdir(join(project_dir, ".paket"))
copy_file_to_project("paket.bootstrapper.exe", ".paket/paket.exe")
copy_file_to_project("src-paket-references", "src/paket.references")
if require_test_project:
copy_file_to_project("paket.dependencies", "paket.dependencies")
copy_file_to_project("test-paket-references", "test/paket.references")
elif no_build:
copy_file_to_project("no-build-paket.dependencies", "paket.dependencies")
else:
copy_file_to_project("single-project-paket.dependencies", "paket.dependencies")
if require_test_project:
# no solution so don't need this
copy_file_to_project("NuGet.config", "NuGet.config")
add_src_fsproj_package_reference("Chessie")
if require_test_project:
add_test_fsproj_package_reference("Chessie")
add_test_fsproj_package_reference("FsCheck")
if not no_build:
copy_file_to_project("build.sh", "build.sh")
os.chmod("build.sh", 0o775)
copy_file_to_project("build.cmd", "build.cmd")
if require_test_project:
copy_file_to_project("build.fsx", "build.fsx")
add_test_run_msbuild_watch_target()
else:
copy_file_to_project("single-project-build.fsx", "build.fsx")
copy_file_to_project("fsharp-gitattributes", ".gitattributes")
copy_file_to_project("fsharp-gitignore", ".gitignore")
copy_file_to_project("LICENSE", "LICENSE")
touch_project_file("README.md")
open(join(project_dir, "README.md"), "w").write("# " + project_name)
os.chmod(".paket/paket.exe", 0o775)
run_cmd(".paket{sep}paket.exe install".format(sep=os.path.sep))
run_cmd(".paket{sep}paket.exe auto-restore on".format(sep=os.path.sep))
if run_git:
run_cmd("git init")
run_cmd('git add "*"')
# OPTIONAL
# - lint in fake (though done by the Ionide 'IDE' plugin) https://github.com/fsprojects/FSharpLint/blob/master/docs/content/FAKE-Task.md
# Lint via FAKE only seems to work with .net framework currently
# Lint via FAKE through CLI ok but need PATH install of FSharpLint.exe which must be manually built
# - documentation/releases/nuget packaging/continuous integration
# - fxcop https://fake.build/todo-fxcop.html - seems WINDOWs only. Gendarme is the mono equivalent.
# - coverage with dotCover (jetbrains) http://fake.build/apidocs/fake-dotcover.html etc.
# All commercial, except OpenCover which is also Windows only.
# - source code formatting - only project rider or visual studio currently. Future Ionide feature.
try:
make_project()
finally:
os.chdir(pwd)