forked from ImaginaryDevelopment/imaginary-hero-designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateDesigner.fsx
153 lines (139 loc) · 5.66 KB
/
CreateDesigner.fsx
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
#load "Reusable.fsx"
open Reusable
let createDesignerItemText relPath name =
sprintf """ <Compile Include="%s">
<DependentUpon>%s.cs</DependentUpon>
</Compile>
"""
relPath name
let createDesigner fullpath ns name init fields =
let preamble ns name =
sprintf """
namespace %s
{
public partial class %s
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = (System.ComponentModel.IContainer)new System.ComponentModel.Container();
"""
ns name
let postAmble init fields =
sprintf """
%s
}
%s
#endregion
}
}"""
init fields
sprintf "%s%s" (preamble ns name) (postAmble init fields)
|> writeText fullpath
type InitState =
|NotFound
|Done
|Progressing of indent:int*startLine:int
type ExtractionResult = {RegularLinesRev:string list;InitLinesRev:string list}
type ExtractionState = {InitState:InitState;Result:ExtractionResult}
let extractInitialize title (lines:string[]) =
let addInitLine line (r:ExtractionResult) = line::r.InitLinesRev
let addRegLine line (r:ExtractionResult) = line::r.RegularLinesRev
let lensRegLine line r = {r with RegularLinesRev= addRegLine line r}
let lensInitLine line r = {r with InitLinesRev= addInitLine line r}
let lensResult (r:ExtractionResult) (s:ExtractionState) : ExtractionState = {s with Result=r}
let lensInitLineS line (x:ExtractionState) : ExtractionState = x.Result |> lensInitLine line |> flip lensResult x
let lensRegLineS line (x:ExtractionState) : ExtractionState = x.Result |> lensRegLine line |> flip lensResult x
({Result={RegularLinesRev=List.empty;InitLinesRev=List.empty};InitState=NotFound}, lines |> withIndexes)
||> Seq.fold(fun state (i,line) ->
match state.InitState with
| NotFound when line.Contains("void InitializeComponent(") ->
if state.Result.InitLinesRev.Length > 0 then failwithf "Found init component at %i when %i line(s) already exist in %s" i state.Result.InitLinesRev.Length title
{InitState=Progressing(line.Length - (line.TrimStart().Length),i);Result={state.Result with InitLinesRev=List.singleton line}}
| NotFound
| Done -> lensRegLineS line state
| Progressing (i,_) when line.Trim() = "}" && line.TrimEnd().Length - 1 = i ->
{state with InitState=Done;Result=lensInitLine line state.Result}
| Progressing _ ->
lensInitLineS line state
)
|> fun x -> x.Result
|> fun x ->
let added = x.InitLinesRev.Length + x.RegularLinesRev.Length
if added <> lines.Length then
failwithf "Failed to pull apart %s: %i + %i = %i <> %i" title x.InitLinesRev.Length x.RegularLinesRev.Length added lines.Length
x
open System.IO
let getDesignerlessForms () =
getAllCsFiles()
|> Seq.choose(fun fp ->
let dp = Path.Combine(Path.GetDirectoryName fp,Path.GetFileNameWithoutExtension fp + ".Designer.cs")
if
not <| fp.EndsWith(".Designer.cs")
&& not <| File.Exists dp
then
Some (fp,dp)
else None
)
|> Seq.choose(fun (fp,dp) ->
let text = readLines fp
text |> Seq.tryFind(fun x -> x.Trim().Contains(": Form"))
|> Option.map(fun _ ->
fp,text,dp
)
)
()
let addDesignerItem pp relative name =
let text = createDesignerItemText relative name
((List.empty,false),readLines pp |> withIndexes)
||> Seq.fold(fun (lines,added) (i,line) ->
if added then (line::lines,true)
elif line.Contains("<ItemGroup>") then
text::line::lines,true
else (line::lines,false)
)
|> fun (lines,added) ->
if not added then failwithf "Could not add %s designer to %s" name pp
lines
|> List.rev
|> String.concat "\r\n"
|> writeText pp
getDesignerlessForms()
|> Seq.map(fun (fp, lines,dp) ->
let name = Path.GetFileNameWithoutExtension fp
match findParentCsProject fp with
| None -> failwithf "Couldn't find parent cs project for %s" fp
| Some pp ->
let relative = getRelativePath {Source=pp;TargetPath=dp}
if(relative.Contains(".cs\\")) || not <| File.Exists(Path.Combine(relative,fp)) then failwithf "Bad relative path made (%s,%s,%s)" relative pp fp
printfn "Made relative %s" relative
addDesignerItem pp relative name
let x = extractInitialize fp lines
(fp, x.RegularLinesRev |> List.rev),(dp,x.InitLinesRev |> List.rev)
)
|> Seq.iter(fun ((fp,lines),(dp,inits)) ->
let name = Path.GetFileNameWithoutExtension fp |> Path.GetFileNameWithoutExtension
createDesigner dp "Hero_Designer" name (String.concat "\r\n" inits) System.String.Empty
writeText fp (String.concat "\r\n" lines)
)
//|> Seq.length