summaryrefslogtreecommitdiff
path: root/projects/generate.vbs
blob: bdb6ef2032d6d23a2c795d8c96fe90a1b4d73c39 (plain)
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
Option Explicit

Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")

' openttd_vs90.sln    is for MSVC 2008
' openttd_vs90.vcproj is for MSVC 2008
' langs_vs90.vcproj   is for MSVC 2008
' strgen_vs90.vcproj  is for MSVC 2008

' openttd_vs80.sln    is for MSVC 2005
' openttd_vs80.vcproj is for MSVC 2005
' langs_vs80.vcproj   is for MSVC 2005
' strgen_vs80.vcproj  is for MSVC 2005

Sub safety_check(filename)
	Dim file, line, regexp, list

	' Define regexp
	Set regexp = New RegExp
	regexp.Pattern = "#|ottdres.rc|win32.cpp|win32_v.cpp"
	regexp.Global = True

	' We use a dictionary to check duplicates
	Set list = CreateObject("Scripting.Dictionary")

	Set file = FSO.OpenTextFile(filename, 1, 0, 0)
	While Not file.AtEndOfStream
		line = Replace(file.ReadLine, Chr(9), "") ' Remove tabs
		If Len(line) > 0 And Not regexp.Test(line) Then
			line = FSO.GetFileName(line)
			if list.Exists(line) Then
				WScript.Echo " !! ERROR !!" _
				& vbCrLf & "" _
				& vbCrLf & "The filename '" & line & "' is already used in this project." _
				& vbCrLf & "Because MSVC uses one single directory for all object files, it" _
				& vbCrLf & "cannot handle filenames with the same name inside the same project." _
				& vbCrLf & "Please rename either one of the file and try generating again." _
				& vbCrLf & "" _
				& vbCrLf & " !! ERROR !!"
				WScript.Quit(1)
			End If
			list.Add line, line
		End If
	Wend
	file.Close
End Sub

Function load_main_data(filename)
	Dim res, file, line, deep, skip, first_time
	res = ""
	' Read the source.list and process it
	Set file = FSO.OpenTextFile(filename, 1, 0, 0)
	While Not file.AtEndOfStream
		line = Replace(file.ReadLine, Chr(9), "") ' Remove tabs
		If Len(line) > 0 Then
			Select Case Split(line, " ")(0)
				Case "#end"
					If deep = skip Then skip = skip - 1
					deep = deep - 1
				Case "#else"
					If deep = skip Then
						skip = skip - 1
					ElseIf deep - 1 = skip Then
						skip = skip + 1
					End If
				Case "#if"
					line = Replace(line, "#if ", "")
					If deep = skip And ( _
						line = "SDL" Or _
						line = "PNG" Or _
						line = "WIN32" Or _
						line = "MSVC" Or _
						line = "DIRECTMUSIC" Or _
						line = "NO_THREADS" _
					) Then skip = skip + 1
					deep = deep + 1
				Case "#"
					if deep = skip Then
						line = Replace(line, "# ", "")
						if first_time <> 0 Then
							res = res & "		</Filter>" & vbCrLf
						Else
							first_time = 1
						End If
						res = res & _
						"		<Filter" & vbCrLf & _
						"			Name=" & Chr(34) & line & Chr(34) & vbCrLf & _
						"			>" & vbCrLf
					End If
				Case Else
					If deep = skip Then
						line = Replace(line, "/" ,"\")
						res = res & _
						"			<File" & vbCrLf & _
						"				RelativePath=" & Chr(34) & ".\..\src\" & line & Chr(34) & vbCrLf & _
						"				>" & vbCrLf & _
						"			</File>" & vbCrLf
					End If
			End Select
		End If
	Wend
	res = res & "		</Filter>"
	file.Close()
	load_main_data = res
End Function

Function load_lang_data(dir)
	Dim res, folder, file
	res = ""
	Set folder = FSO.GetFolder(dir)
	For Each file In folder.Files
		file = FSO.GetFileName(file)
		If FSO.GetExtensionName(file) = "txt" Then
			file = Left(file, Len(file) - 4)
			res = res _
			& vbCrLf & "		<File" _
			& vbCrLf & "			RelativePath=" & Chr(34) & "..\src\lang\" & file & ".txt" & Chr(34) _
			& vbCrLf & "			>" _
			& vbCrLf & "			<FileConfiguration" _
			& vbCrLf & "				Name=" & Chr(34) & "Debug|Win32" & Chr(34) _
			& vbCrLf & "				>" _
			& vbCrLf & "				<Tool" _
			& vbCrLf & "					Name=" & Chr(34) & "VCCustomBuildTool" & Chr(34) _
			& vbCrLf & "					Description=" & Chr(34) & "Generating " & file & " language file" & Chr(34) _
			& vbCrLf & "					CommandLine=" & Chr(34) & "..\objs\strgen\strgen.exe -s ..\src\lang -d ..\bin\lang &quot;$(InputPath)&quot;&#x0D;&#x0A;" & Chr(34) _
			& vbCrLf & "					AdditionalDependencies=" & Chr(34) & Chr(34) _
			& vbCrLf & "					Outputs=" & Chr(34) & "..\bin\lang\" & file & ".lng" & Chr(34) _
			& vbCrLf & "				/>" _
			& vbCrLf & "			</FileConfiguration>" _
			& vbCrLf & "		</File>"
		End If
	Next
	load_lang_data = res
End Function

Sub generate(data, dest)
	Dim srcfile, destfile, line
	WScript.Echo "Generating " & FSO.GetFileName(dest) & "..."
	Set srcfile = FSO.OpenTextFile(dest & ".in", 1, 0, 0)
	Set destfile = FSO.CreateTextFile(dest, -1, 0)

	' Everything above the !!FILES!! marker
	line = srcfile.ReadLine()
	While line <> "!!FILES!!"
		If len(line) > 0 Then destfile.WriteLine(line)
		line = srcfile.ReadLine()
	Wend

	' Our generated content
	destfile.WriteLine(data)

	' Everything below the !!FILES!! marker
	While Not srcfile.AtEndOfStream
		line = srcfile.ReadLine()
		If len(line) > 0 Then destfile.WriteLine(line)
	Wend
	srcfile.Close()
	destfile.Close()
End Sub

Dim ROOT_DIR
ROOT_DIR = FSO.GetFolder("..").Path
If Not FSO.FileExists(ROOT_DIR & "/source.list") Then
	ROOT_DIR = FSO.GetFolder(".").Path
End If
If Not FSO.FileExists(ROOT_DIR & "/source.list") Then
	WScript.Echo "Can't find source.list, needed in order to make this run." _
	& vbCrLf & "Please go to either the project dir, or the root dir of a clean SVN checkout."
	WScript.Quit(1)
End If

safety_check ROOT_DIR & "/source.list"

Dim openttd
openttd = load_main_data(ROOT_DIR &"/source.list")
generate openttd, ROOT_DIR & "/projects/openttd_vs80.vcproj"
generate openttd, ROOT_DIR & "/projects/openttd_vs90.vcproj"

Dim lang
lang = load_lang_data(ROOT_DIR & "/src/lang")
generate lang, ROOT_DIR & "/projects/langs_vs80.vcproj"
generate lang, ROOT_DIR & "/projects/langs_vs90.vcproj"