commit 流程ing

This commit is contained in:
OBKoro1 2019-12-30 21:13:08 +08:00
parent bca29dab30
commit 17652d0617
6 changed files with 364 additions and 63 deletions

View File

@ -28,7 +28,28 @@ body > .el-container {
text-align: center; text-align: center;
} }
.form-title2 {
width: 160px;
text-align: right;
font-weight: bold;
font-size: 24px;
}
.log_form_father{
width: 600px;
}
.el-divider__text, .el-link{
font-size: 20px;
}
.input_form{ .input_form{
width: 200px; width: 200px;
} }
.tip{
margin-left: 160px;
font-size: 17px;
display: flex;
align-items: center;
}

View File

@ -2,7 +2,7 @@
* Author : OBKoro1 * Author : OBKoro1
* Date : 2019-12-26 13:49:02 * Date : 2019-12-26 13:49:02
* LastEditors : OBKoro1 * LastEditors : OBKoro1
* LastEditTime : 2019-12-29 19:01:20 * LastEditTime : 2019-12-30 20:10:46
* FilePath : /autoCommit/src/models/WebView.ts * FilePath : /autoCommit/src/models/WebView.ts
* Description : 创建webview * Description : 创建webview
* https://github.com/OBKoro1 * https://github.com/OBKoro1
@ -37,7 +37,6 @@ class WebView {
// 获取资源地址 // 获取资源地址
// TODO: 打包 // TODO: 打包
const srcPath = process.env.NODE_ENV !== 'production' ? 'src' : 'dist'; const srcPath = process.env.NODE_ENV !== 'production' ? 'src' : 'dist';
console.log('srcPath', srcPath, process.env.NODE_ENV);
this.currentPanel = vscode.window.createWebviewPanel( this.currentPanel = vscode.window.createWebviewPanel(
WebviewPanelOption.type, WebviewPanelOption.type,
WebviewPanelOption.title, WebviewPanelOption.title,
@ -93,9 +92,8 @@ class WebView {
* webview * webview
*/ */
public close() { public close() {
const panel = this.currentPanel; if (!this.currentPanel) return;
if (!panel) return; this.currentPanel.dispose();
panel.dispose();
} }
// webview消息回调 // webview消息回调
@ -107,6 +105,16 @@ class WebView {
} }
showMessage(data, command); showMessage(data, command);
} }
/**
* webview
* @param command
* @param message
*/
public postMessage(command: string, data: object) {
if (!this.currentPanel) return
this.currentPanel.webview.postMessage({ command, data })
}
} }
export default WebView; export default WebView;

152
src/models/commitHandle.ts Normal file
View File

@ -0,0 +1,152 @@
/*
* Author : OBKoro1
* Date : 2019-12-30 16:59:30
* LastEditors : OBKoro1
* LastEditTime : 2019-12-30 20:21:13
* FilePath : /autoCommit/src/models/commitHandle.ts
* Description : commit
* https://github.com/OBKoro1
*/
import { webviewMsg } from '../util/dataStatement';
import * as moment from 'moment';
import * as fs from 'fs';
import { execSync } from 'child_process';
import { RandomNumber } from '../util/util';
import { getPanelWebview, outputLog } from '../util/vscodeUtil';
import WebView from './WebView';
class CommitHandle {
public paramsObj: any;
public timeArr: Array<string>;
public autoCommitView: WebView;
constructor(message: webviewMsg) {
this.paramsObj = message.data;
this.timeArr = [];
this.timeHandle();
this.autoCommitView = getPanelWebview();
}
// 处理所有时间段
timeHandle() {
// 处理所有时间范围
this.paramsObj.formatTime.forEach((item: Array<string>) => {
// 获取每个时间范围的具体日期
let detailTimeArr = this.getAllDay(item[0], item[1]);
// 日期去重
detailTimeArr = detailTimeArr.filter(ele => {
return !this.timeArr.includes(ele);
});
this.timeArr.push(...detailTimeArr);
});
this.sortTime();
}
// 日期排序
sortTime() {
this.timeArr = this.timeArr.sort((item1: string, item2: string): number => {
const dateArr1: Array<any> = item1.split('-');
const dateArr2: Array<any> = item2.split('-');
if (dateArr1[0] === dateArr2[0]) {
if (dateArr1[1] === dateArr2[1]) {
// 日期不同就比较日期
return dateArr1[2] - dateArr2[2];
} else {
// 月份不同就比较月份
return dateArr1[1] - dateArr2[1];
}
} else {
// 年份不同就比较年份
return dateArr1[0] - dateArr2[0];
}
});
this.commitFn();
}
// TODO: 中断
commitFn() {
outputLog('将要commit的日期:', this.timeArr);
outputLog('每个日期提交次数:', this.paramsObj.commitNumber);
let totalNum = 0; // 总commit次数
// 遍历日期
this.timeArr.forEach(item => {
// 每个日期commit次数
for (let i = 0; i < this.paramsObj.commitNumber; i++) {
let time = this.formatTime(item); // 2019-01-02 08:00
time = moment(time).format(); // 2019-01-02T00:00:00+0800
const commitContent = `${time} \n 随机数:${RandomNumber(1, 100000)}`;
fs.writeFileSync(
`${this.paramsObj.itemSrc}/${this.paramsObj.fileSrc}`,
commitContent,
'utf-8'
);
const res = this.myExecSync(
`cd ${this.paramsObj.itemSrc} && git add . && git commit -m 'autoCommit' --date='${time}' && git pull && git push origin master`
);
console.log('res 结束', res);
// 当前最新版本commit 信息
const cmd = `git log -1 \
--date=iso --pretty=format:'{"commit": "%h","author": "%aN <%aE>","date": "%ad","message": "%s"},' \
$@ | \
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
perl -pe 's/},]/}]/'`;
// [{"commit": "a6b5f3d","author": "OBKoro1 <1677593011@qq.com>","date": "2019-12-26 21:05:57 +0800","message": "init"}]
const log = this.myExecSync(cmd);
console.log('log 开始', log);
totalNum++;
outputLog('commit内容', commitContent);
}
outputLog(`总commit次数${totalNum}`);
outputLog('自动commit完成');
});
}
// 格式化日期
formatTime(time: string) {
return `${time} 08:00`;
}
// 获取两个日期之间的间隔: [ '2019-02-02', '2019-02-03' ... ]
getAllDay(begin: string, end: string) {
const timeArr = [];
const beginSplit: Array<string> = begin.split('-');
const endSplit: Array<string> = end.split('-');
const beginDate = new Date();
beginDate.setUTCFullYear(
Number(beginSplit[0]),
Number(beginSplit[1]) - 1,
Number(beginSplit[2])
);
const endDate = new Date();
endDate.setUTCFullYear(
Number(endSplit[0]),
Number(endSplit[1]) - 1,
Number(endSplit[2])
);
const beginNumber = beginDate.getTime();
const endNumber = endDate.getTime();
for (let k: any = beginNumber; k <= endNumber; ) {
const day = new Date(parseInt(k));
const dayFormat = moment(day).format('YYYY-MM-DD');
timeArr.push(dayFormat);
k = k + 24 * 60 * 60 * 1000;
}
return timeArr;
}
myExecSync(cmd: string) {
// 除了该方法直到子进程完全关闭后才返回 执行完毕 返回
try {
const res = execSync(cmd, {
encoding: 'utf8',
timeout: 0,
maxBuffer: 200 * 1024,
killSignal: 'SIGTERM',
cwd: undefined,
env: undefined
});
return res;
} catch (err) {
outputLog(`执行命令出错:${cmd}`);
outputLog(`错误信息:${err}`);
return err;
}
}
}
export default CommitHandle;

View File

@ -2,38 +2,74 @@
* Author : OBKoro1 * Author : OBKoro1
* Date : 2019-12-25 17:08:18 * Date : 2019-12-25 17:08:18
* LastEditors : OBKoro1 * LastEditors : OBKoro1
* LastEditTime : 2019-12-27 14:40:39 * LastEditTime : 2019-12-30 21:02:57
* FilePath : /autoCommit/src/models/index.ts * FilePath : /autoCommit/src/models/index.ts
* Description : 插件逻辑入口 * Description : 插件逻辑入口
* https://github.com/OBKoro1 * https://github.com/OBKoro1
*/ */
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import WebView from './WebView'; import WebView from './WebView';
import { webviewMsg } from '../util/dataStatement' import { webviewMsg } from '../util/dataStatement';
import { setPanelWebview } from '../util/vscodeUtil';
import CommitHandle from './commitHandle';
import { outputLog } from '../util/vscodeUtil';
import * as fs from 'fs';
class ExtensionLogic { class ExtensionLogic {
public readonly context: vscode.ExtensionContext; public readonly context: vscode.ExtensionContext;
public MessageCallBack: any; public MessageCallBack: any;
public autoCommitView: WebView; public autoCommitView: WebView;
public CommitHandle: any;
public constructor(context: vscode.ExtensionContext) { public constructor(context: vscode.ExtensionContext) {
this.context = context; this.context = context;
this.autoCommitView = new WebView(this.context, this.messageCallBack); this.autoCommitView = new WebView(
this.createView() this.context,
this.messageCallBack.bind(this)
);
setPanelWebview(this.autoCommitView);
this.createView();
} }
createView() { createView() {
const option = { const option = {
type: 'autoCommit', type: 'autoCommit',
title: 'Github自动提交commit工具', title: 'Github自动提交commit工具',
fileName: 'autoCommit' fileName: 'autoCommit'
} };
this.autoCommitView.create(option) this.autoCommitView.create(option);
} }
// 处理webview的消息 // 处理webview的消息
private messageCallBack(message: webviewMsg) { private messageCallBack(message: webviewMsg) {
if (message.data.id === 'user-login-message') { if (message.command === 'commit') {
// this.userLogin(message.params); this.CommitHandle = new CommitHandle(message);
} else if (message.command === 'choose-item') {
this.publishChooseFile();
}
}
async publishChooseFile() {
const urlArr: any = await vscode.window.showOpenDialog({
canSelectFiles: false, // 允许选择文件
canSelectFolders: true, // 是否可以选择文件夹
canSelectMany: false // 是否可以选择多个文件
});
const itemSrc = urlArr[0].path;
if (this.hasGit(itemSrc)) {
this.autoCommitView.postMessage('choose item success', itemSrc);
} else {
outputLog('项目地址错误', `${itemSrc}根目录没有.git文件夹`);
}
}
public hasGit(itemSrc: string) {
const url = `${itemSrc}/.git`; // 文件路径
try {
let isDirectory = fs.statSync(url).isDirectory(); // 判断是否为文件夹 返回布尔值
if (isDirectory) {
return true;
} else {
return false;
}
} catch (err) {
return false;
} }
} }
} }

View File

@ -2,45 +2,64 @@
* Author : OBKoro1 * Author : OBKoro1
* Date : 2019-12-25 17:13:30 * Date : 2019-12-25 17:13:30
* LastEditors : OBKoro1 * LastEditors : OBKoro1
* LastEditTime : 2019-12-26 14:51:21 * LastEditTime : 2019-12-30 21:10:57
* FilePath : /autoCommit/src/util/vscodeUtil.ts * FilePath : /autoCommit/src/util/vscodeUtil.ts
* Description : vscode * Description : vscode
* https://github.com/OBKoro1 * https://github.com/OBKoro1
*/ */
import * as vscode from 'vscode' import * as vscode from 'vscode';
let extensionContext = '';
let extensionContext = '' let webview: any;
// 存储插件上下午文 // 存储插件上下午文
function setExtensionContext(context: any) { function setExtensionContext(context: any) {
extensionContext = context; extensionContext = context;
} }
// 获取插件上下文 // 获取插件上下文
function getExtensionContext() { function getExtensionContext() {
return extensionContext return extensionContext;
}
// 存储webview
function setPanelWebview(webviewClass: any) {
webview = webviewClass;
}
// 获取webview
function getPanelWebview() {
return webview;
}
// 输出日志
function outputLog(...arr: any) {
console.log('日志', ...arr);
webview.postMessage('console-log', arr);
} }
// vscode 消息通知 // vscode 消息通知
function showMessage(message: string, type = 'error') { function showMessage(message: string, type = 'error') {
const actions: any = { const actions: any = {
info: () => { info: () => {
vscode.window.showInformationMessage(message) vscode.window.showInformationMessage(message);
}, },
alert: () => { alert: () => {
vscode.window.showWarningMessage(message) vscode.window.showWarningMessage(message);
}, },
error: () => { error: () => {
vscode.window.showErrorMessage(message) vscode.window.showErrorMessage(message);
}
} }
};
actions[type]() actions[type]();
} }
export { export {
showMessage, // vscode 消息通知 outputLog, // 打印日志
setExtensionContext, // 存储插件上下文 getPanelWebview, // 获取webview
getExtensionContext // 获取插件上下文 setPanelWebview, // 存储webview
} showMessage, // vscode 消息通知
setExtensionContext, // 存储插件上下文
getExtensionContext // 获取插件上下文
};

View File

@ -22,7 +22,7 @@
<span>项目地址 :</span> <span>项目地址 :</span>
</el-tooltip> </el-tooltip>
</label> </label>
<span>{{ form.itemSrc }}</span> <span @click="chooseItem">{{ form.itemSrc }}</span>
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item class="form_item" prop="fileName"> <el-form-item class="form_item" prop="fileName">
@ -55,7 +55,7 @@
required: true, message: '日期不能为空', trigger: 'blur' required: true, message: '日期不能为空', trigger: 'blur'
}"> }">
<label slot="label"> <label slot="label">
<!-- TODO: wiki一个日期只能选中一次 去重 --> <!-- TODO: wiki一个日期只能选中一次 去重 sort排序 切割字符串 单独比较 -->
<el-tooltip class="item" content="要commit的时间范围,可多选" placement="top-start"> <el-tooltip class="item" content="要commit的时间范围,可多选" placement="top-start">
<span>commit日期范围{{index+1}} :</span> <span>commit日期范围{{index+1}} :</span>
</el-tooltip> </el-tooltip>
@ -66,14 +66,33 @@
<el-button @click.prevent="removeTime(index)" :disabled="index === 0">删除</el-button> <el-button @click.prevent="removeTime(index)" :disabled="index === 0">删除</el-button>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-button type="primary" @click="submitForm('form')" :disabled="runNow">开始commit</el-button> <el-button type="primary" @click="submitForm('form')" :disabled="runNow !== 1 && runNow!== 0">开始commit</el-button>
<el-button @click="addTimeRange">新增时间范围</el-button> <el-button @click="addTimeRange">新增时间范围</el-button>
<el-button @click="addTimeRange" :disabled="!runNow">取消commit</el-button> <el-button @click="reset" :disabled="runNow === 1">取消commit</el-button>
<el-button @click="clearLog" :disabled="showText.logStr !== ''">清空日志</el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
<div class="form-title2">日志和文档</div>
<el-main> <el-main>
<!-- TODO: 日志和wiki链接 github链接 --> <div class="log_form_father">
<el-input type="textarea" placeholder="请输入内容" v-model="paramsStr" disabled> <el-form label-width="160px">
<el-form-item label="插件运行日志 :">
<el-input @input="formatLog" type="textarea"
:autosize="{ minRows: 1, maxRows: 8}"
placeholder="无法输入 仅用于展示" v-model="showText.logStr" :disabled="runNow === 1 || runNow === 0">
</el-form-item>
<el-form-item label="开始commit的参数 :">
<el-input @input="watchParamsChange" type="textarea"
:autosize="{ minRows: 1, maxRows: 4}"
placeholder="无法输入 仅用于展示" v-model="showText.paramsStr" :disabled="showText.paramsStr === ''">
</el-form-item>
<el-form-item label="wiki文档 :">
<el-link href="https://element.eleme.io" type="primary" target="_blank">Readme</el-link>
<el-link href="https://element.eleme.io" type="primary" target="_blank">更新日志</el-link>
</el-form-item>
<div class="tip">觉得这个插件还不错的话, 请给我点个<el-link href="https://element.eleme.io" type="primary" target="_blank">Star⭐</el-link>吧~</div>
</div>
</el-form>
</el-input> </el-input>
</el-main> </el-main>
</el-main> </el-main>
@ -84,6 +103,7 @@
<script src="../assets/scripts/vue.min.js"></script> <script src="../assets/scripts/vue.min.js"></script>
<script src="../assets/scripts/element2.13.min.js"></script> <script src="../assets/scripts/element2.13.min.js"></script>
<script> <script>
const vscode = acquireVsCodeApi()
new Vue({ new Vue({
el: '#app', el: '#app',
data() { data() {
@ -96,8 +116,13 @@
} }
} }
return { return {
paramsStr: '', // commit参数 showText:{
runNow: false, // 运行中 oldParams: '', // 保存的参数
paramsStr: '', // commit参数
logArr: [], // 保存的日志
logStr: '', //
},
runNow: 0, // 0 初始化 1运行中 2运行结束 3取消运行
form: { form: {
// itemSrc: '/Users/koro/work/web_my/testCommit', // itemSrc: '/Users/koro/work/web_my/testCommit',
itemSrc: '点击选择要commit的项目文件夹', itemSrc: '点击选择要commit的项目文件夹',
@ -139,7 +164,7 @@
}; };
}, },
mounted() { mounted() {
// this.initListener(); this.initListener();
}, },
methods: { methods: {
submitForm(formName) { submitForm(formName) {
@ -154,13 +179,15 @@
// 开始commit // 开始commit
runCommit() { runCommit() {
let params = this.form let params = this.form
params.format = this.mapTimeData() params.formatTime = this.mapTimeData()
this.runNow = true this.runNow = 1
this.paramsStr = JSON.stringify(params) this.showText.paramsStr = JSON.stringify(params)
this.postMessage(params, 'commit'); this.showText.oldParams = this.showText.paramsStr
// this.mockLog()
this.postMessage('commit', params);
}, },
// 发送消息到插件 // 发送消息到插件
postMessage(data, command) { postMessage(command, data) {
vscode.postMessage({ vscode.postMessage({
command, command,
data data
@ -168,20 +195,55 @@
}, },
// 注册 // 注册
initListener() { initListener() {
// TODO: 通信问题 接收不到
window.addEventListener('message', event => { window.addEventListener('message', event => {
console.log('event', event);
const { command, data } = event.data; const { command, data } = event.data;
console.log('登录成功 addEventListener', event); console.log('data',data)
if (command === 'success') { console.log('event', event,command,data);
// 登录成功 // console.log('登录成功 addEventListener', event);
} // if (command === 'success') {
// // 登录成功
// }
}); });
}, },
// 重置: commit成功和中断commit // 重置: commit成功和中断commit
reset() { reset() {
this.runNow = false; // TODO: runNow 3 取消运行
this.paramsStr = '' this.runNow = 2;
this.showText.paramsStr = ''
this.showText.oldParams = ''
},
// 写html时用
mockLog(){
this.showText.logArr = [
'res 开始 undefined',
'res 结束',
'log 开始 [{"commit": "a6b5f3d","author": "OBKoro1 <1677593011@qq.com>","date": "2019-12-26 21:05:57 +0800","message": "init"}]',
'log 结束',
'commit内容 2019-12-27T08:00:00+08:000',
'总commit次数1'
]
let num = 0;
let timeId = setInterval(() => {
if(num< 100){
num++
this.showText.logArr.unshift(`一些文字${num}`)
this.formatLog()
}else{
this.reset()
clearInterval(timeId)
}
}, 1000);
},
clearLog(){
this.showText.logArr = []
this.showText.logStr = ''
},
formatLog(){
this.showText.logStr = this.showText.logArr.join(`\n`)
},
watchParamsChange(){
this.showText.paramsStr = this.showText.oldParams
}, },
// 删除时间 // 删除时间
removeTime(index) { removeTime(index) {
@ -193,6 +255,9 @@
return item.value return item.value
}) })
}, },
chooseItem() {
this.postMessage('choose-item')
},
// 增加时间范围选择 // 增加时间范围选择
addTimeRange() { addTimeRange() {
this.form.timeArr.push({ this.form.timeArr.push({