Skip to content

Commit

Permalink
Merge branch 'tool4d_native_decompress' of github.com:4d/4D-Analyzer-…
Browse files Browse the repository at this point in the history
…VSCode into tool4d_native_decompress
  • Loading branch information
guillaume-kotulski committed Mar 22, 2024
2 parents f17058c + 834701b commit 77527b2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 59 deletions.
6 changes: 5 additions & 1 deletion editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@
{
"command": "4d-analyzer.display4DVersion",
"title": "4D Analyzer: Display 4D version used"
}
},
{
"command": "4d-analyzer.cleanUnusedToolVersions",
"title": "4D Analyzer: Clean all the tool4D previously downloaded"
}
]
},
"scripts": {
Expand Down
62 changes: 23 additions & 39 deletions editor/src/apiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ export function requestLabelVersion(url: string, channel: string): Promise<Label

export class APIManager {
private readonly _apiKey: string;
private readonly _packagePreference: string;
constructor(inAPIKey: string, inPackagePreference: string) {
constructor(inAPIKey: string) {
this._apiKey = inAPIKey;
this._packagePreference = inPackagePreference;
}

public async getLastMajorVersionAvailable(inStartMajorVersion: number, inChannel: string): Promise<number> {
let labelVersion = new LabeledVersion(inStartMajorVersion, 0, 0, 0, false, inChannel, false);
while (true) {
const url = this.getURLTool4D(labelVersion);
const url = this.getURLTool4D(labelVersion, "Windows_NT");
try {
await requestLabelVersion(url, labelVersion.channel);
labelVersion.version += 1;
Expand All @@ -89,15 +87,12 @@ export class APIManager {
Starting from 20R5
Linux has tar.xz and .deb
*/
public getURLTool4D(inVersion: LabeledVersion): string {
public getURLTool4D(inVersion: LabeledVersion, inOS? : string): string {
let url = "https://preprod-product-download.4d.com/release/";
const labeledVersion: LabeledVersion = inVersion;

const version = String(labeledVersion.version);
const releaseVersion = String(labeledVersion.releaseVersion);
const hasLinuxDeb: boolean = this._packagePreference === "deb"
&& (labeledVersion.isMain()
|| (labeledVersion.version >= 20 && labeledVersion.releaseVersion >= 5))

if (labeledVersion.isMain()) {
url += "main/main";
Expand All @@ -116,15 +111,10 @@ export class APIManager {

url += "/latest/";

const type = os.type();
const type = inOS ?? os.type();

if (type == "Linux") {
if (hasLinuxDeb) {
url += `linux/tool4d.deb`;
}
else {
url += `linux/tool4d_Linux.tar.xz`;
}
url += `linux/tool4d.deb`;
}
else if (type == "Darwin") {
const arch = os.arch();
Expand All @@ -136,30 +126,24 @@ export class APIManager {
url += ".tar.xz";
}
else if (type == "Windows_NT") {
url += `win/tool4d_win`;
url += ".tar.xz";
url += `win/tool4d_win.tar.xz`;
}
let hasArgs = false;
if(inVersion.isMain())
{
if (inVersion.isMain()) {
hasArgs = true;
url += "?";
url += "token_tool=" + this._apiKey;
}
else
{
if(!hasArgs)
{
url += "?";
hasArgs = true;
}

if(inVersion.channel === "beta")
{
else {
if (inVersion.channel === "beta") {
if (!hasArgs) {
url += "?";
hasArgs = true;
}
url += "channel=" + inVersion.channel;
}
}

return url;
}

Expand All @@ -185,16 +169,16 @@ export class APIManager {
let labeledVersionCloud;
const url = this.getURLTool4D(labeledVersionWanted);
try {
labeledVersionCloud = await requestLabelVersion(url, labeledVersionWanted.channel);
}
catch (error) {
throw new Error(`Failed url: ${url}`);
}
let isBeta = await this.isCloudVersionABeta(labeledVersionCloud);
labeledVersionCloud = await requestLabelVersion(url, labeledVersionWanted.channel);
}
catch (error) {
throw new Error("Cloud url failed:" + url);
}
let isBeta = await this.isCloudVersionABeta(labeledVersionCloud);

labeledVersionCloud.channel = isBeta ? "beta" : "stable";
return labeledVersionCloud;

labeledVersionCloud.channel = isBeta ? "beta" : "stable";
return labeledVersionCloud;

}

public async downloadVersion(inlabelVersion: LabeledVersion, output: string): Promise<object> {
Expand Down
8 changes: 8 additions & 0 deletions editor/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ export function display4DVersion(ctx: Ctx): Cmd {
}


export function cleanUnusedToolVersions(ctx: Ctx): Cmd {

return async () => {

return await ctx.cleanUnusedToolVersions()
}
}

47 changes: 46 additions & 1 deletion editor/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { workspace } from 'vscode';
import * as child_process from 'child_process';
import * as net from 'net';
import {Logger} from "./logger"
import { existsSync, mkdirSync, readdirSync, rmdirSync, rm } from "fs";
import path = require('path');

export type CommandCallback = {
call: (ctx: Ctx) => Commands.Cmd;
};
Expand Down Expand Up @@ -77,6 +80,47 @@ export class Ctx {
return toolPreparator.prepareTool4D(outLocation);
}

public async cleanUnusedToolVersions() {
const location = path.join(!this._config.tool4DLocation() ? this.extensionContext.globalStorageUri.fsPath : this._config.tool4DLocation(), "tool4d");
if(!this._config.serverPath) //no path are ready
{
rmdirSync(location)
}
else
{
const labeledVersion = this.get4DVersion();
function getDirectories(source: string) {
if (existsSync(source)) {
return readdirSync(source, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
}
return [];
}

let labeledVersionWithoutChangelist = labeledVersion.clone();
labeledVersionWithoutChangelist.changelist = 0;
const directories = getDirectories(location);
directories.forEach(async directory=> {
const currentLabeledFolder = LabeledVersion.fromString(directory);

if(currentLabeledFolder.compare(labeledVersionWithoutChangelist) != 0){
await rm(path.join(location, directory),{recursive:true}, ()=>{})
}
else
{
const directoriesChangelist = getDirectories(path.join(location, directory));
directoriesChangelist.forEach(async dir => {
if(Number(dir)!= labeledVersion.changelist){
await rm(path.join(location, directory, dir),{recursive:true}, ()=>{})
}
})

}
})
}
}

public async downloadLastTool4D(): Promise<ResultUpdate> {
const toolPreparator: ToolPreparator = new ToolPreparator(this._config.tool4DWanted(), this._config.tool4DDownloadChannel(), this._config.tool4dAPIKEY());
const outLocation = !this._config.tool4DLocation() ? this.extensionContext.globalStorageUri.fsPath : this._config.tool4DLocation();
Expand Down Expand Up @@ -205,7 +249,8 @@ export class Ctx {
this._commands = {
filesStatus: { call: Commands.filesStatus },
updateTool4D: { call: Commands.updateTool4D },
display4DVersion: { call: Commands.display4DVersion }
display4DVersion: { call: Commands.display4DVersion },
cleanUnusedToolVersions: { call: Commands.cleanUnusedToolVersions }
};

for (const [name, command] of Object.entries(this._commands)) {
Expand Down
3 changes: 3 additions & 0 deletions editor/src/labeledVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class LabeledVersion {
else if (this.isRRelease && b.isRRelease && this.releaseVersion != b.releaseVersion) {
return this.releaseVersion - b.releaseVersion;
}
else if (this.isRRelease != b.isRRelease && this.releaseVersion != b.releaseVersion) {
return this.releaseVersion - b.releaseVersion;
}
return this.changelist - b.changelist;
}

Expand Down
35 changes: 17 additions & 18 deletions editor/src/toolPreparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LabeledVersion } from './labeledVersion';
import { InfoPlistManager } from './infoplist';
import { window, ProgressLocation } from 'vscode';
import { Logger } from './logger';
import { APIManager} from './apiManager';
import { APIManager } from './apiManager';

export interface ResultUpdate {
path: string;
Expand All @@ -19,16 +19,14 @@ export interface ResultUpdate {

export class ToolPreparator {
private readonly _versionWanted: LabeledVersion;
private readonly _packagePreference: string; //deb | tar
private readonly _APIManager: APIManager;
constructor(inVersion: string, channel: string, inAPIKey: string) {
this._versionWanted = LabeledVersion.fromString(inVersion);
if (this._versionWanted.isLatest()) {
this._versionWanted.main = !!inAPIKey;
}
this._versionWanted.channel = channel
this._packagePreference = this._computePackagePreference();
this._APIManager = new APIManager(inAPIKey, this._packagePreference)
this._APIManager = new APIManager(inAPIKey)
}

private async _decompress(input: string, inDirectory: string): Promise<void> {
Expand Down Expand Up @@ -111,7 +109,6 @@ export class ToolPreparator {

return localLabelVersion;
}
// tool4DExecutable = this._getTool4DExe(path.join(this._getTool4DPath(tool4DMainFolder, labelVersionToGet, true), "tool4d"));

private _getTool4DPath(inRootFolder: string, labeledVersion: LabeledVersion, compute: boolean): string {
let label = labeledVersion;
Expand All @@ -134,27 +131,21 @@ export class ToolPreparator {
tool4DExecutable = path.join(inRootFolder, "tool4d.app");
}
else if (osType === "Linux") {
if (this._packagePreference === "deb") {
tool4DExecutable = "/opt/tool4d/tool4d"
}
else {
tool4DExecutable = path.join(inRootFolder, "bin", "tool4d");
}
tool4DExecutable = "/opt/tool4d/tool4d"
}
return tool4DExecutable;
}

private _computePackagePreference(): string {
let wantTar = "tar";
private _computeSudoRights(): boolean {
if (os.type() === "Linux") {
try {
child_process.execSync("sudo -v", { shell: '/bin/bash', timeout: 100 })
wantTar = "deb"
return true;
} catch (err) {
wantTar = "tar";
return false;
}
}
return wantTar;
return false;
}

private async _prepareLastTool(inPathToStore: string, inUpdateIfNeeded: boolean, inProgress: vscode.Progress<{
Expand All @@ -170,6 +161,8 @@ export class ToolPreparator {
const labelVersionAvailableLocally = this._getTool4DAvailableLocally(tool4DMainFolder, labeledVersionWanted);

Logger.get().log("Version wanted", this._versionWanted)


let lastMajorVersion = labeledVersionWanted.version;
let tool4DExecutable = ""
let labelVersionToGet = labelVersionAvailableLocally;
Expand All @@ -189,10 +182,16 @@ export class ToolPreparator {
throw new Error(`Tool4D ${labeledVersionWanted.toString(false)} does not exist`);
}

if (os.type() === "Linux") {
if(!this._computeSudoRights())
{
throw new Error(`Missing sudo rights`);
}
}

Logger.get().log("Version available cloud", labeledVersionCloud)
Logger.get().log("Version available locally", labelVersionAvailableLocally)
if (labelVersionAvailableLocally.changelist > 0
&& labeledVersionCloud.isRRelease == labelVersionAvailableLocally.isRRelease
&& labeledVersionCloud.compare(labelVersionAvailableLocally) > 0) {
result.updateAvailable = true;
}
Expand All @@ -213,7 +212,7 @@ export class ToolPreparator {

Logger.get().log("Version to get", labelVersionToGet)

if (os.type() === "Linux" && this._packagePreference === "deb") {
if (os.type() === "Linux") {
if (labelVersionToGet.compare(InfoPlistManager.fromExePath(this._getTool4DExe("")).getVersion()) === 0) {
tool4DExecutable = this._getTool4DExe("");
}
Expand Down

0 comments on commit 77527b2

Please sign in to comment.