Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide/gha-update-image/lib/jb-helper/jb-helper.ts
2501 views
1
import semver from "semver";
2
import { ReleaseItem } from "./jb-releases";
3
4
export type UsePlatformVersionType =
5
| "build" // use `build` field from ReleaseItem
6
| "version" // use `version` field from ReleaseItem
7
| "build-snapshot"; // use major `build` with `-EAP-CANDIDATE-SNAPSHOT`
8
9
export const AllProductCodes = ["IIU", "GW", "GO", "PCP", "PS", "RM", "WS", "RD", "CL", "RR"] as const;
10
export type ProductCodes = (typeof AllProductCodes)[number];
11
export const AllProductIDs = [
12
"intellij",
13
"gateway",
14
"goland",
15
"pycharm",
16
"phpstorm",
17
"rubymine",
18
"webstorm",
19
"rider",
20
"clion",
21
"rustrover",
22
] as const;
23
export type ProductIDs = (typeof AllProductIDs)[number];
24
25
export interface TargetInfo {
26
/**
27
* @example intellij goland
28
*/
29
productId: ProductIDs;
30
/**
31
* @example IIU GO
32
*/
33
productCode: ProductCodes;
34
/**
35
* @example eap,rc,release
36
*/
37
productType: string;
38
/**
39
* Examples value for different type:
40
* - build 243.18137.22
41
* - version 2024.3
42
* - build-snapshot 243.18137-EAP-CANDIDATE-SNAPSHOT
43
*/
44
usePlatformVersionType: "build" | "version" | "build-snapshot";
45
gradlePropertiesPath: string;
46
gradlePropertiesTemplate: string;
47
}
48
49
export interface GradleProperties {
50
pluginSinceBuild: string;
51
pluginUntilBuild: string;
52
pluginVerifierIdeVersions: string;
53
platformVersion: string;
54
}
55
56
export const parseGradleProperties = (content: string) => {
57
const properties: Record<string, string> = {};
58
content.split("\n").forEach((line) => {
59
if (line.startsWith("#")) {
60
return;
61
}
62
const [key, value] = line.split("=");
63
if (key && value) {
64
properties[key.trim()] = value.trim();
65
}
66
});
67
return properties as any as GradleProperties;
68
};
69
70
export function parseGradlePropertiesFromTaskConfig(
71
info: Pick<TargetInfo, "usePlatformVersionType">,
72
targetBuild: ReleaseItem,
73
): GradleProperties {
74
const build = targetBuild.build;
75
const buildSem = semver.parse(build);
76
if (!buildSem) {
77
throw new Error(`Invalid build version ${build}`);
78
}
79
let platformVersion = build;
80
switch (info.usePlatformVersionType) {
81
case "build":
82
platformVersion = build;
83
break;
84
case "version":
85
platformVersion = targetBuild.version;
86
break;
87
case "build-snapshot":
88
platformVersion = `${buildSem.major}.${buildSem.minor}-EAP-CANDIDATE-SNAPSHOT`;
89
break;
90
}
91
return {
92
pluginSinceBuild: `${buildSem.major}.${buildSem.minor}`,
93
pluginUntilBuild: `${buildSem.major}.*`,
94
pluginVerifierIdeVersions: targetBuild.majorVersion,
95
platformVersion,
96
};
97
}
98
99
export function renderPropertiesTemplate(
100
whoami: string,
101
info: Pick<TargetInfo, "gradlePropertiesTemplate">,
102
properties: GradleProperties,
103
) {
104
const { gradlePropertiesTemplate } = info;
105
let newContent = gradlePropertiesTemplate;
106
Object.entries(properties).forEach(([key, value]) => {
107
newContent = newContent.replace(`{{${key}}}`, value);
108
});
109
return `# Code generated by ${whoami}. DO NOT EDIT.\n${newContent}`;
110
}
111
112
export const maybeCompatible = (
113
newRelease: Pick<ReleaseItem, "build">,
114
oldVersion: Pick<GradleProperties, "pluginSinceBuild">,
115
) => {
116
const sinceBuild = semver.parse(`${oldVersion.pluginSinceBuild}.0`);
117
const newBuild = semver.parse(newRelease.build);
118
if (!sinceBuild || !newBuild) {
119
return false;
120
}
121
const versionOK = semver.gte(newBuild, sinceBuild, false);
122
const majorOK = semver.major(newBuild) === semver.major(sinceBuild);
123
return versionOK && majorOK;
124
};
125
126