This repository has been archived by the owner on Mar 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
141 lines (135 loc) · 3.72 KB
/
Gruntfile.js
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
// pico8Grunt
// https://github.com/TeamNoComplyGames/pico8Grunt
// by @torch2424
// LICENSE: Apache 2.0
// https://oss.ninja/apache-2.0/torch2424
module.exports = function(grunt) {
//Define our source code files path
var srcPath = 'src/**/*.lua';
var cartCodePath = 'luaCartCode.lua';
var buildTasks = [
'concat',
'replace:noMultiLineComments',
'replace:noComments',
'replace:noEmptyLines',
'replace:operatorWhiteSpace',
'replace:singleSpace',
'replace:blockIndentation',
'replace:pico'
];
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
dist: {
src: [srcPath],
dest: cartCodePath,
},
},
replace: {
noComments: {
src: [cartCodePath],
dest: cartCodePath,
replacements: [{
from: /--.*/g,
to: function (matchedContent) {
return '';
}
}]
},
noMultiLineComments: {
src: [cartCodePath],
dest: cartCodePath,
replacements: [{
from: /(-- *\[\[)([\s\S]*?)(-- *\]\])/g,
to: function (matchedContent) {
return '';
}
}]
},
noEmptyLines: {
src: [cartCodePath],
dest: cartCodePath,
replacements: [{
from: /^\s*$\n/gm,
to: function (matchedContent) {
// The above regex matches empty lines, and the \n that creates the next one
// Which allows us to remove the preceeding \n
return '';
}
}]
},
operatorWhiteSpace: {
src: [cartCodePath],
dest: cartCodePath,
replacements: [{
from: /\s*[,+=><\-*/]+\s/g,
to: function (matchedContent) {
// Return the matched content without spaces
return matchedContent.replace(/\s/g, '');
}
}]
},
singleSpace: {
src: [cartCodePath],
dest: cartCodePath,
replacements: [{
from: / +/g,
to: function (matchedContent) {
// Return the matched content without spaces
return matchedContent.replace(/ +/g, ' ');
}
}]
},
blockIndentation: {
src: [cartCodePath],
dest: cartCodePath,
replacements: [{
from: /\n\s+/g,
to: function (matchedContent) {
// Return the matched content without spaces
return matchedContent.replace(/\n\s+/g, '\n');
}
}]
},
pico: {
src: ['cart.p8'],
dest: 'cart.p8',
replacements: [{
from: /(__lua__)(.*|[\s\S]*)(__gfx__)/g,
to: function (matchedContent) {
//Get our first two strings
//Lua header needs newline, gfx does not
var luaHeader = '__lua__\n';
var gfxHeader = '__gfx__';
var cartCode = grunt.file.read(cartCodePath);
return luaHeader + cartCode + gfxHeader;
}
}]
}
},
watch: {
pico: {
files: [srcPath],
tasks: buildTasks,
options: {
spawn: false,
}
}
}
});
//Regex to match correct lines of code in .p8 file
//https://regex101.com/r/eZ1gT7/688
// /(__lua__)(.*|[\s\S]*)(__gfx__)/g
// Concatenation of files task
grunt.loadNpmTasks('grunt-contrib-concat');
//Text replace for the original .p8 file
grunt.loadNpmTasks('grunt-text-replace');
//Watch for file changes
grunt.loadNpmTasks('grunt-contrib-watch');
// Register our tasks
grunt.registerTask('default', buildTasks);
grunt.registerTask('build', buildTasks);
//Alternative for 'grunt watch'
grunt.registerTask('serve', ['watch']);
};