Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 46a94f1

Browse files
authored
Merge pull request #5 from Dot-lua/v6-classify
V6 classify
2 parents d3e33fc + 0dd2d40 commit 46a94f1

File tree

40 files changed

+1011
-1136
lines changed

40 files changed

+1011
-1136
lines changed
File renamed without changes.

Test/src/Main/js/Test/Main2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a"

Test/src/Main/package.info.json

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
{
2-
"Id": "TypeWriterTest",
3-
"Name": "TypeWriter Test",
4-
"Description": "A Test for TypeWriter",
5-
"Version": "1.0.0",
6-
"Authors": {
7-
"Developers": [
8-
"CoreByte"
9-
],
10-
"Contributors": []
11-
},
12-
"Contact": {
13-
"Website": "",
14-
"Source": "",
15-
"Socials": {}
16-
},
17-
"Dependencies": [
18-
19-
"npm+types/[email protected]",
20-
21-
],
22-
"Entrypoints": {
23-
"Main": "Test"
24-
}
2+
"Id": "TypeWriterTest",
3+
"Name": "TypeWriter Test",
4+
"Description": "A Test for TypeWriter",
5+
"Version": "1.0.0",
6+
"Authors": {
7+
"Developers": [
8+
"CoreByte"
9+
],
10+
"Contributors": []
11+
},
12+
"Contact": {
13+
"Website": "",
14+
"Source": "",
15+
"Socials": {}
16+
},
17+
"Dependencies": [
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
],
34+
"Entrypoints": {
35+
"Main": "Test"
36+
}
2537
}

TypeWriter/Actions/build/Index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
const BuildHelper = require("../../Lib/BuildHelper.js")
1+
const BuilderClass = require("../../Classes/Builder.js")
22

33
module.exports.Name = "Build"
44
module.exports.Execute = async function() {
55
const InputPath = TypeWriter.Arguments.input
66
const InputBranch = TypeWriter.Arguments.branch
7+
const OutputPath = TypeWriter.Arguments.output
78

8-
const BuildId = BuildHelper.Build(InputPath, InputBranch)
9-
BuildHelper.CompressBuild(BuildId, TypeWriter.Arguments.output)
10-
BuildHelper.CleanupBuild(BuildId)
9+
const Builder = new BuilderClass(InputPath, InputBranch, OutputPath)
10+
await Builder.ValidatePackageInfo()
11+
await Builder.ScanCode()
12+
await Builder.ScanResources()
13+
await Builder.Compress()
14+
await Builder.Cleanup()
1115
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
const FS = require("fs-extra")
2-
const RandomString = require("randomstring")
32
const Path = require("path")
43
const LoadEnvoirment = require("../../Lib/LoadEnvoirment")
4+
const RandomString = require("../../Lib/RandomString.js")
5+
56

67
module.exports.Name = "Execute"
78
module.exports.Execute = async function() {
89
const InputPath = TypeWriter.Arguments.input
9-
const ExecuteId = RandomString.generate(32)
10+
const ExecuteId = RandomString(32)
1011
const ExecuteFolder = Path.normalize(`${TypeWriter.Folder}/Cache/ExecuteCache/${ExecuteId}/`)
12+
FS.mkdirSync(ExecuteFolder)
1113

1214
TypeWriter.Logger.Debug(`Input is ${InputPath}`)
1315
TypeWriter.Logger.Debug(`ExecuteId is ${ExecuteId}`)
1416
TypeWriter.Logger.Debug(`ExecuteFolder is ${ExecuteFolder}`)
1517

16-
FS.mkdirSync(ExecuteFolder)
17-
1818
await LoadEnvoirment(ExecuteFolder)
19-
const Package = TypeWriter.LoadFile(InputPath)
20-
TypeWriter.LoadEntrypointAsync(Package.Id, "Main")
19+
const Package = await TypeWriter.PackageManager.LoadPackage(OutputFile)
20+
await Package.LoadEntrypoint("Main")
2121
}

TypeWriter/Actions/run/Index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
const RandomString = require("randomstring")
21
const FS = require("fs-extra")
32
const Path = require("path")
43

5-
const BuildHelper = require("../../Lib/BuildHelper.js")
4+
const BuilderClass = require("../../Classes/Builder.js")
65
const LoadEnvoirment = require("../../Lib/LoadEnvoirment")
6+
const RandomString = require("../../Lib/RandomString.js")
77

88
module.exports.Name = "Run"
99
module.exports.Execute = async function() {
1010
const InputPath = TypeWriter.Arguments.input
1111
const InputBranch = TypeWriter.Arguments.branch
1212

13-
const BuildId = BuildHelper.Build(InputPath, InputBranch)
14-
const ExecuteId = RandomString.generate(32)
13+
const ExecuteId = RandomString(32)
14+
const ExecuteFolder = Path.normalize(`${TypeWriter.Folders.Cache.ExecuteCacheFolder}/${ExecuteId}/`)
15+
FS.mkdirSync(ExecuteFolder)
16+
1517
TypeWriter.Logger.Debug(`ExecuteId is ${ExecuteId}`)
16-
const ExecuteFolder = Path.normalize(`${TypeWriter.Folder}/Cache/ExecuteCache/${ExecuteId}/`)
1718
TypeWriter.Logger.Debug(`ExecuteFolder is ${ExecuteFolder}`)
18-
19-
FS.mkdirSync(ExecuteFolder)
2019

21-
BuildHelper.CompressBuild(BuildId, ExecuteFolder)
22-
BuildHelper.CleanupBuild(BuildId)
23-
if (BuildId == false) {
24-
return
25-
}
20+
const Builder = new BuilderClass(InputPath, InputBranch, ExecuteFolder)
21+
await Builder.ValidatePackageInfo()
22+
await Builder.ScanCode()
23+
await Builder.ScanResources()
24+
const OutputFile = await Builder.Compress()
25+
await Builder.Cleanup()
2626

2727
await LoadEnvoirment(ExecuteFolder)
28-
const Package = TypeWriter.LoadFile(`${ExecuteFolder}/${FS.readdirSync(ExecuteFolder)}`)
29-
TypeWriter.LoadEntrypointAsync(Package.Id, "Main")
28+
const Package = await TypeWriter.PackageManager.LoadPackage(OutputFile)
29+
await Package.LoadEntrypoint("Main")
3030
}

TypeWriter/Assets/package.info.json

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"Developers": [
99
"CoreByte"
1010
],
11-
"Contributors": [
12-
13-
]
11+
"Contributors": []
1412
},
1513

1614
"Contact": {
@@ -19,14 +17,9 @@
1917
"Socials": {}
2018
},
2119

22-
"Dependencies": [
23-
{
24-
"Source": "LIT",
25-
"Package": "coro-spawn",
26-
"Version": null,
27-
"Type": "Packaged"
28-
}
29-
],
20+
"Dependencies": [],
21+
22+
"Preload": [],
3023

3124
"Entrypoints": {
3225
"Main": "me.corebyte.test"

TypeWriter/Classes/Builder.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
const FS = require("fs-extra")
2+
const Path = require("path")
3+
const KlawSync = require('klaw-sync')
4+
const RandomString = require("../Lib/RandomString.js")
5+
const DependencyParser = require("../Lib/DependencyParser.js")
6+
const Tar = require("tar")
7+
8+
async function FixDependencyVersions(Dependencies) {
9+
let Updated = false
10+
const VersionFixes = []
11+
12+
let DependencyIndex = -1
13+
for (const Dependency of Dependencies) {
14+
let ParsedDependency = DependencyParser.Parse(Dependency)
15+
DependencyIndex++
16+
17+
if (ParsedDependency.Version != undefined) { continue }
18+
19+
Updated = true
20+
TypeWriter.Logger.Warning(`No version specified for ${ParsedDependency.FullName}, fixing...`)
21+
VersionFixes.push(
22+
[
23+
DependencyIndex,
24+
ParsedDependency,
25+
async function (DependencyIndex, ParsedDependency) {
26+
const LatestVersion = await TypeWriter.DependencyManager.GetLatestVersion(ParsedDependency)
27+
ParsedDependency.Version = LatestVersion
28+
Dependencies[DependencyIndex] = DependencyParser.Format(ParsedDependency)
29+
}
30+
]
31+
32+
)
33+
34+
}
35+
36+
await Promise.all(VersionFixes.map(D => D[2](D[0], D[1])))
37+
38+
return [Updated, Dependencies]
39+
}
40+
41+
async function ScanCode(ScanFolder, Extension) {
42+
ScanFolder = Path.normalize(ScanFolder)
43+
const ScannedCode = {}
44+
const Files = KlawSync(ScanFolder, { nodir: true })
45+
for (const File of Files) {
46+
const FilePath = File.path
47+
const CodePath = FilePath.split(ScanFolder)[1].replaceAll("\\", "/").replaceAll("/", ".").substring(1).split(".").slice(0, -1).join(".")
48+
49+
ScannedCode[CodePath] = {
50+
Type: Extension,
51+
Code: encodeURIComponent(FS.readFileSync(FilePath, "utf-8"))
52+
}
53+
54+
if (CodePath.endsWith(".Main")) {
55+
ScannedCode[CodePath.substring(0, CodePath.length - 5)] = {
56+
Type: "Redirect",
57+
Path: CodePath
58+
}
59+
} else if (CodePath.endsWith(".Index")) {
60+
ScannedCode[CodePath.substring(0, CodePath.length - 6)] = {
61+
Type: "Redirect",
62+
Path: CodePath
63+
}
64+
}
65+
}
66+
67+
return ScannedCode
68+
}
69+
70+
class Builder {
71+
constructor(Folder, Branch, OutputPath) {
72+
this.BuildFolder = Folder
73+
this.BranchFolder = Path.normalize(`${Folder}/${Branch}/`)
74+
this.OutputPath = OutputPath
75+
this.BuildId = RandomString(32)
76+
this.BuildFolder = Path.normalize(`${TypeWriter.Folders.Cache.BuildCacheFolder}/${this.BuildId}/`)
77+
this.PackageInfoFile = Path.normalize(`${this.BranchFolder}/package.info.json`)
78+
79+
TypeWriter.Logger.Debug(`Building to ${this.BuildFolder}`)
80+
81+
FS.ensureDirSync(this.BuildFolder)
82+
FS.cpSync(this.BranchFolder + "/package.info.json", this.BuildFolder + "/package.info.json")
83+
84+
this.CreateRequiredFolders()
85+
}
86+
87+
CreateRequiredFolders() {
88+
FS.ensureDirSync(this.BranchFolder + "/js")
89+
FS.ensureDirSync(this.BranchFolder + "/lua")
90+
FS.ensureDirSync(this.BranchFolder + "/resources")
91+
}
92+
93+
async ValidatePackageInfo() {
94+
const PackageInfo = FS.readJsonSync(this.PackageInfoFile)
95+
let NeedsUpdate = false
96+
97+
const [Updated, Dependencies] = await FixDependencyVersions(PackageInfo.Dependencies)
98+
NeedsUpdate = Updated || NeedsUpdate
99+
PackageInfo.Dependencies = Dependencies
100+
101+
if (NeedsUpdate) {
102+
FS.writeJsonSync(this.PackageInfoFile, PackageInfo, { spaces: 4 })
103+
FS.writeJSONSync(this.BuildFolder + "/package.info.json", PackageInfo, { spaces: 4 })
104+
}
105+
}
106+
107+
async ScanCode() {
108+
const ScannedCode = Object.assign(
109+
{},
110+
await ScanCode(this.BranchFolder + "/lua", "lua"),
111+
await ScanCode(this.BranchFolder + "/js", "js")
112+
)
113+
114+
FS.writeJSONSync(
115+
`${this.BuildFolder}/Code.json`,
116+
ScannedCode,
117+
{
118+
spaces: "\t"
119+
}
120+
)
121+
}
122+
123+
async ScanResources() {
124+
const ResourceFolder = Path.normalize(`${this.BranchFolder}/resources`)
125+
const DestinationResourceFolder = `${this.BuildFolder}/resources/`
126+
127+
const ResourceIndex = KlawSync(
128+
this.BranchFolder + "/resources",
129+
{
130+
nodir: true
131+
}
132+
).map(
133+
(File) => {
134+
const FilePath = File.path
135+
const ResourceFilePath = Path.normalize(FilePath.split(ResourceFolder)[1]).replaceAll("\\", "/")
136+
return ResourceFilePath
137+
}
138+
)
139+
140+
FS.copySync(ResourceFolder, DestinationResourceFolder)
141+
FS.writeJSONSync(
142+
`${this.BuildFolder}/ResourceIndex.json`,
143+
ResourceIndex,
144+
{
145+
spaces: "\t"
146+
}
147+
)
148+
}
149+
150+
async Compress() {
151+
const PackageInfo = FS.readJsonSync(this.PackageInfoFile)
152+
const OutputFile = Path.join(this.OutputPath + `/${PackageInfo.Id}.twr`)
153+
TypeWriter.Logger.Debug(`Outputting to ${OutputFile} in ${this.BuildFolder}`)
154+
Tar.create(
155+
{
156+
file: OutputFile,
157+
cwd: this.BuildFolder,
158+
sync: true,
159+
noMtime: true,
160+
portable: true
161+
},
162+
FS.readdirSync(this.BuildFolder)
163+
)
164+
165+
return OutputFile
166+
}
167+
168+
async Cleanup() {
169+
FS.rmSync(this.BuildFolder, { recursive: true, force: true })
170+
}
171+
}
172+
173+
module.exports = Builder

0 commit comments

Comments
 (0)