Skip to content

Commit

Permalink
Merge pull request #260 from CommandDash/fix/rename_perm
Browse files Browse the repository at this point in the history
fix: fixes permission issue when renaming executable for windows
  • Loading branch information
samyakkkk authored Apr 23, 2024
2 parents 473d190 + fb5d55e commit 087854c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
7 changes: 4 additions & 3 deletions vscode/src/repository/http-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export async function refreshAccessToken(refreshToken: string): Promise<string>


export async function downloadFile(url: string, destinationPath: string, onProgress: (progress: number) => void): Promise<void> {
/// First download on a temp path. This prevents from converting partial downloaded files (due to interruption) into executables.
const tempFilePath = `${destinationPath}.tmp`;

const response = await axios({
Expand Down Expand Up @@ -97,6 +98,6 @@ export async function downloadFile(url: string, destinationPath: string, onProgr
writer.on('finish', resolve);
writer.on('error', reject);
});

fs.renameSync(tempFilePath, destinationPath); // Only write file to destination path once download finishes
}
// Downloaded executable is saved as a pre-downloaded file which will be renamed in the next session.
fs.renameSync(tempFilePath, `${destinationPath}.pre-downloaded`);
}
28 changes: 17 additions & 11 deletions vscode/src/utilities/commanddash-integration/dart-cli-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as child_process from 'child_process';
import { EventEmitter } from 'events';
import { Task } from './task';
import { join } from 'path';
import { chmod, existsSync, unlink } from 'fs';
import { chmod, chmodSync, existsSync, renameSync, unlink } from 'fs';
import { downloadFile, makeHttpRequest } from '../../repository/http-utils';
import { AxiosRequestConfig } from 'axios';
import * as vscode from 'vscode';
Expand All @@ -26,16 +26,6 @@ async function setupExecutable(clientVersion: string, executablePath: string, ex
return;
}
await downloadFile(response['url'], executablePath, onProgress);
if (platform === 'darwin' || platform === 'linux') {
// Downloaded file is required to be coverted to an executable.
return new Promise<void>((resolve, reject) => {
chmod(executablePath, '755', (err) => {
if (err) { reject(err); }
console.log('The permissions for the executable have been set');
resolve();
});
});
}
}

export async function deleteExecutable(executablePath: string): Promise<void> {
Expand Down Expand Up @@ -115,8 +105,24 @@ export class DartCLIClient {
await deleteExecutable(this.executablePath);
}

private renameTempToExecutable(tempFilePath: string) {
renameSync(tempFilePath, this.executablePath);
const platform = os.platform();
if (platform === 'darwin' || platform === 'linux') {
// Downloaded file is required to be coverted to an executable.
chmodSync(this.executablePath, '755');
}
}

public connect() {
// Verify the presence of the temporary file, indicating a downloaded update during the last IDE session.
// Proceed with updating the executable if applicable.
const tempFilePath = `${this.executablePath}.pre-downloaded`;
if (existsSync(tempFilePath)) {
this.renameTempToExecutable(tempFilePath);
}


this.proc = child_process.spawn(this.executablePath, ['process']);

let buffer = '';
Expand Down

0 comments on commit 087854c

Please sign in to comment.