Skip to content

Commit a64de17

Browse files
fix: use missing google proto files in CLI
1 parent 4189b08 commit a64de17

File tree

1 file changed

+102
-57
lines changed

1 file changed

+102
-57
lines changed

arduino-ide-extension/scripts/generate-protocol.js

+102-57
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
const { mkdirSync, promises: fs, rmSync } = require('node:fs');
77
const { exec } = require('./utils');
88
const { glob } = require('glob');
9-
const { SemVer, gte, valid: validSemVer } = require('semver');
9+
const { SemVer, gte, valid: validSemVer, gt } = require('semver');
1010
// Use a node-protoc fork until apple arm32 is supported
1111
// https://github.com/YePpHa/node-protoc/pull/10
1212
const protoc = path.dirname(require('@pingghost/protoc/protoc'));
13-
const repository = await fs.mkdtemp(path.join(os.tmpdir(), 'arduino-cli-'));
1413

1514
const { owner, repo, commitish } = (() => {
1615
const pkg = require(path.join(__dirname, '..', 'package.json'));
@@ -57,11 +56,6 @@
5756
return { owner, repo, commitish };
5857
})();
5958

60-
const url = `https://github.com/${owner}/${repo}.git`;
61-
console.log(`>>> Cloning repository from '${url}'...`);
62-
exec('git', ['clone', url, repository], { logStdout: true });
63-
console.log(`<<< Repository cloned.`);
64-
6559
const { platform } = process;
6660
const resourcesFolder = path.join(
6761
__dirname,
@@ -87,76 +81,127 @@
8781
// - `git-snapshot` for local build executed via `task build`. We do not do this.
8882
// - rest, we assume it is a valid semver and has the corresponding tagged code, we use the tag to generate the APIs from the `proto` files.
8983
/*
90-
{
91-
"Application": "arduino-cli",
92-
"VersionString": "nightly-20210126",
93-
"Commit": "079bb6c6",
94-
"Status": "alpha",
95-
"Date": "2021-01-26T01:46:31Z"
96-
}
97-
*/
84+
{
85+
"Application": "arduino-cli",
86+
"VersionString": "nightly-20210126",
87+
"Commit": "079bb6c6",
88+
"Status": "alpha",
89+
"Date": "2021-01-26T01:46:31Z"
90+
}
91+
*/
9892
const versionObject = JSON.parse(versionJson);
99-
let version = versionObject.VersionString;
100-
if (validSemVer(version)) {
101-
// https://github.com/arduino/arduino-cli/pull/2374
102-
if (gte(new SemVer(version, { loose: true }), new SemVer('0.35.0-rc.1'))) {
103-
version = `v${version}`;
93+
const version = versionObject.VersionString;
94+
95+
// Clone the repository and check out the tagged version
96+
// Return folder with proto files
97+
async function getProtoPath(forceCliVersion) {
98+
const repository = await fs.mkdtemp(path.join(os.tmpdir(), 'arduino-cli-'));
99+
100+
const url = `https://github.com/${owner}/${repo}.git`;
101+
console.log(`>>> Cloning repository from '${url}'...`);
102+
exec('git', ['clone', url, repository], { logStdout: true });
103+
console.log(`<<< Repository cloned.`);
104+
105+
let cliVersion = forceCliVersion || version;
106+
if (validSemVer(cliVersion)) {
107+
// https://github.com/arduino/arduino-cli/pull/2374
108+
if (
109+
gte(new SemVer(version, { loose: true }), new SemVer('0.35.0-rc.1'))
110+
) {
111+
cliVersion = `v${cliVersion}`;
112+
}
113+
console.log(`>>> Checking out tagged version: '${cliVersion}'...`);
114+
exec('git', ['-C', repository, 'fetch', '--all', '--tags'], {
115+
logStdout: true,
116+
});
117+
exec(
118+
'git',
119+
['-C', repository, 'checkout', `tags/${cliVersion}`, '-b', cliVersion],
120+
{ logStdout: true }
121+
);
122+
console.log(`<<< Checked out tagged version: '${cliVersion}'.`);
123+
} else if (forceCliVersion) {
124+
console.log(`WARN: invalid semver: '${forceCliVersion}'.`);
125+
// If the forced version is invalid, do not proceed with fallbacks.
126+
return undefined;
127+
} else if (commitish) {
128+
console.log(
129+
`>>> Checking out commitish from 'package.json': '${commitish}'...`
130+
);
131+
exec('git', ['-C', repository, 'checkout', commitish], {
132+
logStdout: true,
133+
});
134+
console.log(
135+
`<<< Checked out commitish from 'package.json': '${commitish}'.`
136+
);
137+
} else if (versionObject.Commit) {
138+
console.log(
139+
`>>> Checking out commitish from the CLI: '${versionObject.Commit}'...`
140+
);
141+
exec('git', ['-C', repository, 'checkout', versionObject.Commit], {
142+
logStdout: true,
143+
});
144+
console.log(
145+
`<<< Checked out commitish from the CLI: '${versionObject.Commit}'.`
146+
);
147+
} else {
148+
console.log(
149+
`WARN: no 'git checkout'. Generating from the HEAD revision.`
150+
);
104151
}
105-
console.log(`>>> Checking out tagged version: '${version}'...`);
106-
exec('git', ['-C', repository, 'fetch', '--all', '--tags'], {
107-
logStdout: true,
108-
});
109-
exec(
110-
'git',
111-
['-C', repository, 'checkout', `tags/${version}`, '-b', version],
112-
{ logStdout: true }
113-
);
114-
console.log(`<<< Checked out tagged version: '${version}'.`);
115-
} else if (commitish) {
116-
console.log(
117-
`>>> Checking out commitish from 'package.json': '${commitish}'...`
118-
);
119-
exec('git', ['-C', repository, 'checkout', commitish], { logStdout: true });
120-
console.log(
121-
`<<< Checked out commitish from 'package.json': '${commitish}'.`
122-
);
123-
} else if (versionObject.Commit) {
124-
console.log(
125-
`>>> Checking out commitish from the CLI: '${versionObject.Commit}'...`
126-
);
127-
exec('git', ['-C', repository, 'checkout', versionObject.Commit], {
128-
logStdout: true,
129-
});
130-
console.log(
131-
`<<< Checked out commitish from the CLI: '${versionObject.Commit}'.`
132-
);
133-
} else {
134-
console.log(`WARN: no 'git checkout'. Generating from the HEAD revision.`);
152+
153+
return path.join(repository, 'rpc');
154+
}
155+
156+
const protoPath = await getProtoPath();
157+
158+
if (!protoPath) {
159+
console.log(`Could not find the proto files folder.`);
160+
process.exit(1);
135161
}
136162

137163
console.log('>>> Generating TS/JS API from:');
138-
exec('git', ['-C', repository, 'rev-parse', '--abbrev-ref', 'HEAD'], {
164+
exec('git', ['-C', protoPath, 'rev-parse', '--abbrev-ref', 'HEAD'], {
139165
logStdout: true,
140166
});
141167

142-
const rpc = path.join(repository, 'rpc');
143168
const out = path.join(__dirname, '..', 'src', 'node', 'cli-protocol');
144169
// Must wipe the gen output folder. Otherwise, dangling service implementation remain in IDE2 code,
145170
// although it has been removed from the proto file.
146171
// For example, https://github.com/arduino/arduino-cli/commit/50a8bf5c3e61d5b661ccfcd6a055e82eeb510859.
147172
rmSync(out, { recursive: true, maxRetries: 5, force: true });
148173
mkdirSync(out, { recursive: true });
149174

175+
if (gt(new SemVer(version, { loose: true }), new SemVer('1.0.4'))) {
176+
// Patch for https://github.com/arduino/arduino-cli/issues/2755
177+
// Credit https://github.com/dankeboy36/ardunno-cli-gen/pull/9/commits/64a5ac89aae605249261c8ceff7255655ecfafca
178+
// Download the 1.0.4 version and use the missing google/rpc/status.proto file.
179+
console.log('<<< Generating missing google proto files');
180+
const v104ProtoPath = await getProtoPath('1.0.4');
181+
if (!v104ProtoPath) {
182+
console.log(`Could not find the proto files folder for version 1.0.4.`);
183+
process.exit(1);
184+
}
185+
await fs.cp(
186+
path.join(v104ProtoPath, 'google'),
187+
path.join(protoPath, 'google'),
188+
{
189+
recursive: true,
190+
}
191+
);
192+
console.log(`>>> Generated missing google file`);
193+
}
194+
150195
let protos = [];
151196
try {
152-
const matches = await glob('**/*.proto', { cwd: rpc });
153-
protos = matches.map((filename) => path.join(rpc, filename));
197+
const matches = await glob('**/*.proto', { cwd: protoPath });
198+
protos = matches.map((filename) => path.join(protoPath, filename));
154199
} catch (error) {
155200
console.log(error.stack ?? error.message);
156201
}
157202

158203
if (!protos || protos.length === 0) {
159-
console.log(`Could not find any .proto files under ${rpc}.`);
204+
console.log(`Could not find any .proto files under ${protoPath}.`);
160205
process.exit(1);
161206
}
162207

@@ -167,7 +212,7 @@
167212
`--js_out=import_style=commonjs,binary:${out}`,
168213
`--grpc_out=generate_package_definition:${out}`,
169214
'-I',
170-
rpc,
215+
protoPath,
171216
...protos,
172217
],
173218
{ logStdout: true }
@@ -186,7 +231,7 @@
186231
)}`,
187232
`--ts_out=generate_package_definition:${out}`,
188233
'-I',
189-
rpc,
234+
protoPath,
190235
...protos,
191236
],
192237
{ logStdout: true }

0 commit comments

Comments
 (0)