support to generate project from maven archetype

This commit is contained in:
Yan Zhang 2017-11-22 16:55:43 +08:00 коммит произвёл Yan Zhang
Родитель e2bb8025fa
Коммит ecb44f8efb
5 изменённых файлов: 106 добавлений и 3 удалений

Просмотреть файл

@ -3,7 +3,11 @@ All notable changes to the "vscode-maven" extension will be documented in this f
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
## [Unreleased]
## Unreleased
- Add context menu on `pom.xml`.
- Support maven archetype generate.
## Released
### 0.0.6
- Run command in dedicated terminals for each maven project.

Просмотреть файл

@ -98,6 +98,16 @@
"command": "mavenProject.openPom",
"title": "Open POM file",
"category": "Maven"
},
{
"command": "mavenArchetype.generate",
"title": "Generate from Maven Archetype",
"category": "Maven"
},
{
"command": "mavenArchetype.updateCache",
"title": "Update Maven Archetype Catalog",
"category": "Maven"
}
],
"views": {
@ -113,6 +123,9 @@
{
"command": "mavenArchetype.generate"
},
{
"command": "mavenArchetype.updateCache"
},
{
"command": "mavenProject.effectivePom",
"when": "resourceFilename == pom.xml"

Просмотреть файл

@ -87,6 +87,27 @@ export function activate(context: vscode.ExtensionContext) {
});
});
let commandMavenArchetypeGenerate = vscode.commands.registerCommand("mavenArchetype.generate", () => {
vscode.window.showWorkspaceFolderPick().then(ret => { console.log(ret); });
const archetypeList = Utils.getArchetypeList();
vscode.window.showQuickPick(archetypeList, { matchOnDescription: true }).then(selected => {
if (selected) {
const { artifactId, groupId } = selected;
vscode.window.showQuickPick(selected.versions).then(version => {
if (version) {
Utils.runInTerminal(`mvn archetype:generate -DarchetypeArtifactId=${artifactId} -DarchetypeGroupId=${groupId} -DarchetypeVersion=${version}`, true, 'Maven');
}
});
}
});
});
let commandMavenArchetypeUpdateCache = vscode.commands.registerCommand("mavenArchetype.updateCache", () => {
vscode.window.showInputBox({ value: "http://repo.maven.apache.org/maven2/archetype-catalog.xml" }).then(url => {
vscode.window.setStatusBarMessage("Updating archetype catalog ... ", Utils.updateArchetypeCache(url));
});
});
['clean', 'validate', 'compile', 'test', 'package', 'verify', 'install', 'site', 'deploy'].forEach(goal => {
let commandMavenGoal = vscode.commands.registerCommand(`mavenGoal.${goal}`, (goalItem) => {
const item = goalItem as MavenProjectTreeItem;
@ -99,6 +120,8 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(commandMavenGoalExecute);
context.subscriptions.push(commandMavenProjectEffectivePom);
context.subscriptions.push(commandMavenProjectOpenPom);
context.subscriptions.push(commandMavenArchetypeGenerate);
context.subscriptions.push(commandMavenArchetypeUpdateCache);
}
// this method is called when your extension is deactivated

16
src/mavenArchetype.ts Normal file
Просмотреть файл

@ -0,0 +1,16 @@
import * as vscode from "vscode";
import { QuickPickItem } from "vscode";
export class MavenArchetype implements QuickPickItem {
label: string;
description: string;
artifactId: string;
groupId: string;
versions: string[];
constructor(aid: string, gid: string, desc?: string) {
this.artifactId = aid;
this.groupId = gid;
this.versions = [];
this.label = `${gid}:${aid}`;
this.description = desc;
}
}

Просмотреть файл

@ -1,11 +1,14 @@
import * as vscode from "vscode";
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as http from 'http';
import * as os from 'os';
import * as md5 from "md5";
import * as path from 'path';
import * as xml2js from 'xml2js';
import { MavenProjectTreeItem } from "./mavenProjectTreeItem";
import { MavenArchetype } from "./mavenArchetype";
import { existsSync } from "fs";
@ -105,6 +108,50 @@ export class Utils {
return path.join(os.tmpdir(), "vscode-maven", md5(pomXmlFilePath), 'commandHistory.txt');
}
static getArchetypeList(): MavenArchetype[] {
const localArchetypeXmlFilePath = this.getLocalArchetypeCatalogFilePath();
if (existsSync(localArchetypeXmlFilePath)) {
const xml = fs.readFileSync(localArchetypeXmlFilePath, 'utf8');
let catalog = null;
xml2js.parseString(xml, { explicitArray: false }, (err, res) => { catalog = res; });
if (catalog && catalog['archetype-catalog'] && catalog['archetype-catalog'].archetypes) {
let dict: { [key: string]: MavenArchetype } = {};
catalog['archetype-catalog'].archetypes.archetype.forEach(archetype => {
const identifier = `${archetype.groupId}:${archetype.artifactId}`;
if (!dict[identifier]) {
dict[identifier] = new MavenArchetype(archetype.artifactId, archetype.groupId, archetype.description);
}
if (dict[identifier].versions.indexOf(archetype.version) < 0) {
dict[identifier].versions.push(archetype.version);
}
});
return Object.keys(dict).map(k => dict[k]);
}
}
return [];
}
public static getLocalArchetypeCatalogFilePath(): string {
return path.join(os.homedir(), ".m2", "repository", "archetype-catalog.xml");
}
public static updateArchetypeCache(url: string): Promise<void> {
const filepath = this.getLocalArchetypeCatalogFilePath();
this.mkdirp(path.dirname(filepath));
const file = fs.createWriteStream(filepath);
let ret = new Promise<void>((resolve, reject) => {
const request = http.get(url, (response) => {
response.pipe(file);
response.on("end", () => {
resolve();
});
});
request.on("error", e => {
reject();
});
});
return ret;
}
private static mkdirp(filepath) {
if (fs.existsSync(filepath)) {
return;