-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGruntfile.js
More file actions
147 lines (124 loc) · 4.77 KB
/
Copy pathGruntfile.js
File metadata and controls
147 lines (124 loc) · 4.77 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
const fs = require('fs-extra');
const ini = require('ini');
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-bump');
grunt.initConfig({
bump: {
options: {
files: [
'package.json',
'*/package.json',
'packages/*/package.json',
],
commit: false,
push: false,
createTag: false,
taskList: ['python-bump'],
},
},
});
grunt.registerTask(
'python-bump',
'Bump the version in setup.cfg',
function () {
const opts = this.options({
file: 'cli/setup.cfg',
});
const filePath = opts.file;
// get version from package.json
const packageJson = grunt.file.readJSON('package.json');
const version = packageJson.version;
if (!version) {
grunt.fail.warn('No version specified for bump.');
return;
}
if (!fs.existsSync(filePath)) {
grunt.fail.warn(`File not found: ${filePath}`);
return;
}
try {
const data = fs.readFileSync(filePath, 'utf8');
// the ini/npm parser doesnt support multiline values, which is required for setup.cfg
// Split the data into lines
const lines = data.split(/\r?\n/);
// Find and return the first line that starts with "version"
const versionLine = lines.find((line) =>
line.trim().toLowerCase().startsWith('version'),
);
if (!versionLine) {
grunt.fail.warn('No version found in setup.cfg');
return;
}
// Replace the version in the line
const newVersionLine = versionLine.replace(
/=.*$/,
`= ${version}`,
);
// Replace the old version line with the new one
const newData = data.replace(versionLine, newVersionLine);
// Write the new data back to the file
fs.writeFileSync(filePath, newData, 'utf8');
grunt.log.writeln(
`Version bumped to ${version} in ${filePath}`,
);
} catch (err) {
grunt.fail.warn(`Error processing file: ${err.message}`);
}
},
);
// Register a new composite task that runs 'bump' and then 'pyproject'
grunt.registerTask('bumpVersion', function (target) {
if (!target) {
target = 'patch'; // default to 'patch' if no target is specified
}
grunt.task.run(`bump:${target}`);
grunt.task.run('python-bump');
});
grunt.registerTask('validateVersions', function () {
// check if all versions are the same
const packageJson = grunt.file.readJSON('package.json');
const version = packageJson.version;
console.log(`Checking all versions match ${version}`);
const files = grunt.file.expand([
'*/package.json',
'packages/*/package.json',
]);
files.push('package.json');
files.forEach((file) => {
const data = grunt.file.readJSON(file);
if (data.version !== version) {
grunt.fail.warn(
`Version mismatch: ${file} has version ${data.version} instead of ${version}`,
);
} else {
console.log(`Version in ${file} matches ${version}`);
}
});
// check if pyproject.toml has the same version
const filePath = 'cli/setup.cfg';
if (!fs.existsSync(filePath)) {
grunt.fail.warn(`File not found: ${filePath}`);
return;
}
const data = fs.readFileSync(filePath, 'utf8');
// the ini/npm parser doesnt support multiline values, which is required for setup.cfg
// Split the data into lines
const lines = data.split(/\r?\n/);
// Find and return the first line that starts with "version"
const versionLine = lines.find((line) =>
line.trim().toLowerCase().startsWith('version'),
);
if (!versionLine) {
grunt.fail.warn('No version found in setup.cfg');
return;
}
cfg_version = versionLine.split('=')[1].trim();
if (cfg_version !== version) {
grunt.fail.warn(
`Version mismatch: ${filePath} has version ${cfg_version} instead of ${version}`,
);
} else {
console.log(`Version in ${filePath} matches ${version}`);
}
});
};