Skip to content

Commit

Permalink
Prompt the user to fetch packages if missing/out of date
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTup committed Feb 15, 2018
1 parent d62e8ea commit b7f1948
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Dart Code can be [installed from the Visual Studio Code Marketplace](https://mar
- `dart.lineLength`: The maximum length of a line of code. This is used by the document formatter. Defaults to `80`.
- `dart.pubAdditionalArgs`: Additional args to pass to `pub get` and `pub upgrade` commands (eg. `--packages-dir`).
- `dart.runPubGetOnPubspecChanges`: Whether to automatically run `pub get` whenever pubspec.yaml is saved. Defaults to `true`.
- `dart.promptToFetchPackages`: Whether to prompt to fetch packages when opening a project with out of date packages. Defaults to `true`.
- `dart.sdkPath`: If the Dart SDK is not automatically found on your machine from your `PATH` you can enter the path to it here.
- `dart.sdkPaths`: If you often switch between multiple Dart SDKs, setting this option to an array of Dart SDK folders or folders that contain multiple Dart SDKs in sub-folders will allow fast switching by clicking the Dart SDK version in the status bar.
- `dart.showLintNames`: Whether to show the names of linter rules in the problems panel to make it easier to `// ignore:`.
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@
"description": "Automatically runs `pub get` whenever pubspec.yaml is saved.",
"scope": "resource"
},
"dart.promptToFetchPackages": {
"type": "boolean",
"default": true,
"description": "Prompt to fetch packages when opening a project with out of date packages.",
"scope": "resource"
},
"dart.showLintNames": {
"type": "boolean",
"default": false,
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ResourceConfig {
get runPubGetOnPubspecChanges() { return this.getConfig<boolean>("runPubGetOnPubspecChanges"); }
get flutterRunLogFile() { return resolveHomePath(this.getConfig<string>("flutterRunLogFile")); }
get observatoryLogFile() { return resolveHomePath(this.getConfig<string>("observatoryLogFile")); }
get promptToFetchPackages() { return this.getConfig<boolean>("promptToFetchPackages"); }
}

export class CodeCapabilities {
Expand Down
12 changes: 12 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { promptUserForConfigs } from "./user_config_prompts";
import { DartPackageFileContentProvider } from "./providers/dart_package_file_content_provider";
import { ClosingLabelsDecorations } from "./decorations/closing_labels_decorations";
import { DebugConfigProvider, DART_CLI_DEBUG_TYPE, FLUTTER_DEBUG_TYPE } from "./providers/debug_config_provider";
import { isPubGetProbablyRequired, promptToRunPubGet } from "./pub/pub";
import { WorkspaceFolder } from "vscode";

const DART_MODE: vs.DocumentFilter[] = [{ language: "dart", scheme: "file" }, { language: "dart", scheme: "dart-package" }];
const HTML_MODE: vs.DocumentFilter[] = [{ language: "html", scheme: "file" }, { language: "html", scheme: "dart-package" }];
Expand Down Expand Up @@ -305,6 +307,16 @@ export function activate(context: vs.ExtensionContext) {
// Turn on all the commands.
setCommandVisiblity(true, sdks.projectType);

// Prompt for pub get if required
function checkForPackages() {
const folders = util.getDartWorkspaceFolders();
const foldersRequiringPackageFetch = folders.filter((ws: WorkspaceFolder) => config.for(ws.uri).promptToFetchPackages).filter(isPubGetProbablyRequired);
if (foldersRequiringPackageFetch.length > 0)
promptToRunPubGet(foldersRequiringPackageFetch);
}
context.subscriptions.push(vs.workspace.onDidChangeWorkspaceFolders((f) => checkForPackages()));
checkForPackages();

// Log how long all this startup took.
const extensionEndTime = new Date();
analytics.logExtensionStartup(extensionEndTime.getTime() - extensionStartTime.getTime());
Expand Down
54 changes: 54 additions & 0 deletions src/pub/pub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";

import * as path from "path";
import * as fs from "fs";
import { window, commands, Uri, WorkspaceFolder } from "vscode";
import { ProjectType } from "../utils";

export function isPubGetProbablyRequired(ws: WorkspaceFolder): boolean {
const folder = ws.uri.fsPath;
const pubspecPath = path.join(folder, "pubspec.yaml");
const packagesPath = path.join(folder, ".packages");
if (!folder || !fs.existsSync(pubspecPath)) {
console.log("no pubspec");
return false;
}

// If we don't appear to have deps listed in pubspec, then no point prompting.
const regex = new RegExp("dependencies\\s*:", "i");
if (!regex.test(fs.readFileSync(pubspecPath).toString())) {
console.log("no deps");
return false;
}

// If we don't have .packages, we probably need running.
if (!fs.existsSync(packagesPath)) {
console.log("no packages");
return true;
}

const pubspecModified = fs.statSync(pubspecPath).mtime;
const packagesModified = fs.statSync(packagesPath).mtime;

console.log(pubspecModified + " vs " + packagesModified);

return pubspecModified > packagesModified;
}

export function promptToRunPubGet(folders: WorkspaceFolder[]) {
const label = "Fetch packages";
window.showInformationMessage("Some packages are missing or out of date, would you like to fetch them now?", label).then((clickedButton) => {
if (clickedButton === label)
fetchPackages(folders);
});
}

function fetchPackages(folders: WorkspaceFolder[]) {
let task = commands.executeCommand("dart.fetchPackages", folders[0].uri);
for (let i = 1; i < folders.length; i++) {
task = task.then((code) => {
if (code === 0) // Continue with next one only if success
return commands.executeCommand("dart.fetchPackages", folders[i].uri);
});
}
}

0 comments on commit b7f1948

Please sign in to comment.