Skip to content

WIP: Support for git repository #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@
"default": "",
"description": "%ext.config.gist%"
},
"sync.repoEnabled": {
"type": "boolean",
"default": false,
"description": "%ext.config.repoEnabled%"
},
"sync.repoPush": {
"type": "boolean",
"default": true,
"description": "%ext.config.repoPush%"
},
"sync.repoUrl": {
"type": "string",
"default": null,
"description": "%ext.config.repoUrl%"
},
"sync.autoDownload": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -171,6 +186,7 @@
"fs-extra": "^7.0.0",
"https-proxy-agent": "^2.2.1",
"lockfile": "^1.0.4",
"simple-git": "^1.107.0",
"temp": "^0.8.3"
}
}
7 changes: 7 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"ext.config.title": "Code Settings Sync Configuration Settings",
"ext.config.gist": "GitHub GIST ID for Settings Sync.",
"ext.config.repoEnabled": "Use git repository instead of gist.",
"ext.config.repoPush": "Set it false to save configuration only locally.",
"ext.config.repoUrl": "Provide remote url to push changes.",
"ext.config.lastUpload": "Settings Sync last upload date. Set it as empty if you want to manually hit download.",
"ext.config.lastDownload": "Settings Sync last download date. Set it as empty if you want to manually hit download.",
"ext.config.autoDownload": "Set it true to Auto Download the settings on code start. [Code Restart Required]",
Expand Down Expand Up @@ -86,6 +89,10 @@
"common.error.invalidGistId": "Sync : Invalid Gist Id Entered. Verify your gist : https://gist.github.com/<your_userName>/<gist_id>.",
"common.error.tokenNotSave": "Sync : Token Not Saved.",
"common.error.gistNotSave": "Sync : Gist Not Saved.",
"common.error.noGit": "Sync : Git is not installed",
"common.error.noGitUser": "Sync : Git user or email is not configured",
"common.error.noGitRepoUrl": "Sync : Git repository is not set",
"common.error.fetchFirst": "Sync : Remote repository has diffrent settings. Please download first and then retry",
"common.action.openExtPage": "Open Extension Page",
"common.action.openExtTutorial": "Open Tutorial",
"common.action.releaseNotes": "Release Notes",
Expand Down
40 changes: 28 additions & 12 deletions src/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,35 @@ export default class Commons {
} else if (error.code === 4) {
message = localize("common.error.canNotSave");
} else if (error.message) {
try {
message = JSON.parse(error.message).message;
if (message.toLowerCase() === "bad credentials") {
msgBox = true;
message = localize("common.error.invalidToken");
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/settings/tokens'));
}
if (message.toLowerCase() === "not found") {
msgBox = true;
message = localize("common.error.invalidGistId");
if (error.message.indexOf("spawn git ENOENT") !== -1) {
msgBox = true;
message = localize("common.error.noGit");
} else if (error.message.indexOf("Please tell me who you are") !== -1) {
msgBox = true;
message = localize("common.error.noGitUser");
} else if (
error.message.indexOf("No configured push destination") !== -1
) {
msgBox = true;
message = localize("common.error.noGitRepoUrl");
} else if (error.message.indexOf("fetch first") !== -1) {
msgBox = true;
message = localize("common.error.fetchFirst");
} else {
try {
message = JSON.parse(error.message).message;
if (message.toLowerCase() === "bad credentials") {
msgBox = true;
message = localize("common.error.invalidToken");
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/settings/tokens'));
}
if (message.toLowerCase() === "not found") {
msgBox = true;
message = localize("common.error.invalidGistId");
}
} catch (error) {
// message = error.message;
}
} catch (error) {
// message = error.message;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/service/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export class File {
public filePath: string,
public gistName: string
) {}

public Write(): Promise<boolean> {
return FileService.WriteFile(this.filePath, this.content)
}

public ResolvePath(parentDir: string): void {
this.filePath = parentDir + path.sep + this.fileName
}
}
export class FileService {
public static CUSTOMIZED_SYNC_PREFIX = "|customized_sync|";
Expand Down
50 changes: 50 additions & 0 deletions src/service/localGitService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";

import * as git from "simple-git/promise";
import { File } from "./fileService";

export class LocalGitService {
private git: git.SimpleGit = null;

constructor(userPath: string) {
this.git = git(userPath);
}

public async Init(remoteUrl: string): Promise<void> {
await this._setupGit();
await this._setupRemote(remoteUrl);
}

public async Add(files: File[]): Promise<void> {
await this.git.add(files.map(file => file.filePath));
}

public async Commit(message: string): Promise<void> {
await this.git.commit(message);
}

public async Push(): Promise<void> {
await this.git.push("origin", "master", { "--set-upstream": null });
}

private async _setupGit(): Promise<void> {
if (!(await this.git.checkIsRepo())) {
await this.git.init();
}
}

private async _setupRemote(remoteUrl: string): Promise<void> {
if (remoteUrl) {
const remote = await this.git
.getRemotes(true)
.then(remotes => remotes.filter(v => v.name === "origin").shift());
if (remote) {
if (remote.refs.push === remoteUrl) {
return;
}
await this.git.removeRemote("origin");
}
await this.git.addRemote("origin", remoteUrl);
}
}
}
3 changes: 3 additions & 0 deletions src/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Environment } from "./environmentPath";

export class ExtensionConfig {
public gist: string = null;
public repoEnabled: boolean = false;
public repoPush: boolean = true;
public repoUrl: string = null;
public quietSync: boolean = false;
public removeExtensions: boolean = true;
public syncExtensions: boolean = true;
Expand Down
142 changes: 84 additions & 58 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "./setting";

import PragmaUtil from "./pragmaUtil";
import { LocalGitService } from "./service/localGitService";

export class Sync {
constructor(private context: vscode.ExtensionContext) {}
Expand Down Expand Up @@ -69,6 +70,7 @@ export class Sync {
const env = new Environment(this.context);
const common = new Commons(env, this.context);
let github: GitHubService = null;
let localgit: LocalGitService = null;
let localConfig: LocalConfig = new LocalConfig();
const allSettingFiles: File[] = [];
let uploadedExtensions: ExtensionInformation[] = [];
Expand All @@ -89,6 +91,9 @@ export class Sync {
localConfig.customConfig.token,
localConfig.customConfig.githubEnterpriseUrl
);

localgit = new LocalGitService(env.USER_FOLDER);

// ignoreSettings = await common.GetIgnoredSettings(localConfig.customConfig.ignoreUploadSettings);
await startGitProcess(localConfig.extConfig, localConfig.customConfig);
// await common.SetIgnoredSettings(ignoreSettings);
Expand All @@ -106,7 +111,7 @@ export class Sync {
2000
);

if (customSettings.downloadPublicGist) {
if (!syncSetting.repoEnabled && customSettings.downloadPublicGist) {
if (customSettings.token == null || customSettings.token === "") {
vscode.window.showInformationMessage(
localize("cmd.updateSettings.warning.noToken")
Expand Down Expand Up @@ -152,7 +157,7 @@ export class Sync {
extensionFilePath,
extensionFileName
);
allSettingFiles.push(extensionFile);
extensionFile.Write();
}

let contentFiles: File[] = [];
Expand Down Expand Up @@ -238,78 +243,99 @@ export class Sync {
const fileName: string = env.FILE_CLOUDSETTINGS_NAME;
const fileContent: string = JSON.stringify(extProp);
const file: File = new File(fileName, fileContent, "", fileName);
file.ResolvePath(env.USER_FOLDER);
file.Write();
allSettingFiles.push(file);

let completed: boolean = false;
let newGIST: boolean = false;
try {
if (syncSetting.gist == null || syncSetting.gist === "") {
if (customSettings.askGistName) {
customSettings.gistDescription = await common.AskGistName();
}
newGIST = true;
const gistID = await github.CreateEmptyGIST(
localConfig.publicGist,
customSettings.gistDescription
if (syncSetting.repoEnabled) {
vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.uploadingFile"),
3000
);
if (gistID) {
syncSetting.gist = gistID;
vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.newGistCreated"),
2000
await localgit.Init(syncSetting.repoUrl);
await localgit.Add(allSettingFiles);
await localgit.Commit(dateNow.toString());
if (syncSetting.repoUrl && syncSetting.repoPush) {
await localgit.Push();
}
completed = true;
} else {
if (syncSetting.gist == null || syncSetting.gist === "") {
if (customSettings.askGistName) {
customSettings.gistDescription = await common.AskGistName();
}
newGIST = true;
const gistID = await github.CreateEmptyGIST(
localConfig.publicGist,
customSettings.gistDescription
);
} else {
vscode.window.showInformationMessage(
localize("cmd.updateSettings.error.newGistCreateFail")
if (gistID) {
syncSetting.gist = gistID;
vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.newGistCreated"),
2000
);
} else {
vscode.window.showInformationMessage(
localize("cmd.updateSettings.error.newGistCreateFail")
);
return;
}
}
let gistObj = await github.ReadGist(syncSetting.gist);
if (!gistObj) {
vscode.window.showErrorMessage(
localize(
"cmd.updateSettings.error.readGistFail",
syncSetting.gist
)
);
return;
}
}
let gistObj = await github.ReadGist(syncSetting.gist);
if (!gistObj) {
vscode.window.showErrorMessage(
localize("cmd.updateSettings.error.readGistFail", syncSetting.gist)
);
return;
}

if (gistObj.data.owner !== null) {
const gistOwnerName: string = gistObj.data.owner.login.trim();
if (github.userName != null) {
const userName: string = github.userName.trim();
if (gistOwnerName !== userName) {
Commons.LogException(
null,
"Sync : You cant edit GIST for user : " +
gistObj.data.owner.login,
true,
() => {
console.log("Sync : Current User : " + "'" + userName + "'");
console.log(
"Sync : Gist Owner User : " + "'" + gistOwnerName + "'"
);
}
);
return;
if (gistObj.data.owner !== null) {
const gistOwnerName: string = gistObj.data.owner.login.trim();
if (github.userName != null) {
const userName: string = github.userName.trim();
if (gistOwnerName !== userName) {
Commons.LogException(
null,
"Sync : You cant edit GIST for user : " +
gistObj.data.owner.login,
true,
() => {
console.log(
"Sync : Current User : " + "'" + userName + "'"
);
console.log(
"Sync : Gist Owner User : " + "'" + gistOwnerName + "'"
);
}
);
return;
}
}
}
}

if (gistObj.public === true) {
localConfig.publicGist = true;
}
if (gistObj.public === true) {
localConfig.publicGist = true;
}

vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.uploadingFile"),
3000
);
gistObj = github.UpdateGIST(gistObj, allSettingFiles);
completed = await github.SaveGIST(gistObj.data);
if (!completed) {
vscode.window.showErrorMessage(
localize("cmd.updateSettings.error.gistNotSave")
vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.uploadingFile"),
3000
);
return;
gistObj = github.UpdateGIST(gistObj, allSettingFiles);
completed = await github.SaveGIST(gistObj.data);
if (!completed) {
vscode.window.showErrorMessage(
localize("cmd.updateSettings.error.gistNotSave")
);
return;
}
}
} catch (err) {
Commons.LogException(err, common.ERROR_MESSAGE, true);
Expand Down