mirror of
https://github.com/OBKoro1/autoCommit.git
synced 2024-11-25 11:59:23 +08:00
commit 流程ing
This commit is contained in:
parent
bca29dab30
commit
17652d0617
@ -28,7 +28,28 @@ body > .el-container {
|
||||
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{
|
||||
width: 200px;
|
||||
}
|
||||
|
||||
.tip{
|
||||
margin-left: 160px;
|
||||
font-size: 17px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
* Author : OBKoro1
|
||||
* Date : 2019-12-26 13:49:02
|
||||
* LastEditors : OBKoro1
|
||||
* LastEditTime : 2019-12-29 19:01:20
|
||||
* LastEditTime : 2019-12-30 20:10:46
|
||||
* FilePath : /autoCommit/src/models/WebView.ts
|
||||
* Description : 创建webview
|
||||
* https://github.com/OBKoro1
|
||||
@ -37,7 +37,6 @@ class WebView {
|
||||
// 获取资源地址
|
||||
// TODO: 打包
|
||||
const srcPath = process.env.NODE_ENV !== 'production' ? 'src' : 'dist';
|
||||
console.log('srcPath', srcPath, process.env.NODE_ENV);
|
||||
this.currentPanel = vscode.window.createWebviewPanel(
|
||||
WebviewPanelOption.type,
|
||||
WebviewPanelOption.title,
|
||||
@ -93,9 +92,8 @@ class WebView {
|
||||
* 关闭webview
|
||||
*/
|
||||
public close() {
|
||||
const panel = this.currentPanel;
|
||||
if (!panel) return;
|
||||
panel.dispose();
|
||||
if (!this.currentPanel) return;
|
||||
this.currentPanel.dispose();
|
||||
}
|
||||
|
||||
// webview消息回调
|
||||
@ -107,6 +105,16 @@ class WebView {
|
||||
}
|
||||
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;
|
||||
|
152
src/models/commitHandle.ts
Normal file
152
src/models/commitHandle.ts
Normal 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;
|
@ -2,38 +2,74 @@
|
||||
* Author : OBKoro1
|
||||
* Date : 2019-12-25 17:08:18
|
||||
* LastEditors : OBKoro1
|
||||
* LastEditTime : 2019-12-27 14:40:39
|
||||
* LastEditTime : 2019-12-30 21:02:57
|
||||
* FilePath : /autoCommit/src/models/index.ts
|
||||
* Description : 插件逻辑入口
|
||||
* https://github.com/OBKoro1
|
||||
*/
|
||||
import * as vscode from 'vscode';
|
||||
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 {
|
||||
public readonly context: vscode.ExtensionContext;
|
||||
public MessageCallBack: any;
|
||||
public autoCommitView: WebView;
|
||||
public CommitHandle: any;
|
||||
|
||||
public constructor(context: vscode.ExtensionContext) {
|
||||
this.context = context;
|
||||
this.autoCommitView = new WebView(this.context, this.messageCallBack);
|
||||
this.createView()
|
||||
this.autoCommitView = new WebView(
|
||||
this.context,
|
||||
this.messageCallBack.bind(this)
|
||||
);
|
||||
setPanelWebview(this.autoCommitView);
|
||||
this.createView();
|
||||
}
|
||||
createView() {
|
||||
const option = {
|
||||
type: 'autoCommit',
|
||||
title: 'Github自动提交commit工具',
|
||||
fileName: 'autoCommit'
|
||||
}
|
||||
this.autoCommitView.create(option)
|
||||
};
|
||||
this.autoCommitView.create(option);
|
||||
}
|
||||
// 处理webview的消息
|
||||
private messageCallBack(message: webviewMsg) {
|
||||
if (message.data.id === 'user-login-message') {
|
||||
// this.userLogin(message.params);
|
||||
if (message.command === 'commit') {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,15 +2,15 @@
|
||||
* Author : OBKoro1
|
||||
* Date : 2019-12-25 17:13:30
|
||||
* LastEditors : OBKoro1
|
||||
* LastEditTime : 2019-12-26 14:51:21
|
||||
* LastEditTime : 2019-12-30 21:10:57
|
||||
* FilePath : /autoCommit/src/util/vscodeUtil.ts
|
||||
* Description : vscode 相关的公共方法
|
||||
* 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) {
|
||||
@ -19,28 +19,47 @@ function setExtensionContext(context: any) {
|
||||
|
||||
// 获取插件上下文
|
||||
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 消息通知
|
||||
function showMessage(message: string, type = 'error') {
|
||||
const actions: any = {
|
||||
info: () => {
|
||||
vscode.window.showInformationMessage(message)
|
||||
vscode.window.showInformationMessage(message);
|
||||
},
|
||||
alert: () => {
|
||||
vscode.window.showWarningMessage(message)
|
||||
vscode.window.showWarningMessage(message);
|
||||
},
|
||||
error: () => {
|
||||
vscode.window.showErrorMessage(message)
|
||||
}
|
||||
vscode.window.showErrorMessage(message);
|
||||
}
|
||||
};
|
||||
|
||||
actions[type]()
|
||||
actions[type]();
|
||||
}
|
||||
|
||||
export {
|
||||
outputLog, // 打印日志
|
||||
getPanelWebview, // 获取webview
|
||||
setPanelWebview, // 存储webview
|
||||
showMessage, // vscode 消息通知
|
||||
setExtensionContext, // 存储插件上下文
|
||||
getExtensionContext // 获取插件上下文
|
||||
}
|
||||
};
|
||||
|
@ -22,7 +22,7 @@
|
||||
<span>项目地址 :</span>
|
||||
</el-tooltip>
|
||||
</label>
|
||||
<span>{{ form.itemSrc }}</span>
|
||||
<span @click="chooseItem">{{ form.itemSrc }}</span>
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
<el-form-item class="form_item" prop="fileName">
|
||||
@ -55,7 +55,7 @@
|
||||
required: true, message: '日期不能为空', trigger: 'blur'
|
||||
}">
|
||||
<label slot="label">
|
||||
<!-- TODO: wiki:一个日期只能选中一次 去重 -->
|
||||
<!-- TODO: wiki:一个日期只能选中一次 去重 sort排序 切割字符串 单独比较 -->
|
||||
<el-tooltip class="item" content="要commit的时间范围,可多选" placement="top-start">
|
||||
<span>commit日期范围{{index+1}} :</span>
|
||||
</el-tooltip>
|
||||
@ -66,14 +66,33 @@
|
||||
<el-button @click.prevent="removeTime(index)" :disabled="index === 0">删除</el-button>
|
||||
</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" :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>
|
||||
<div class="form-title2">日志和文档</div>
|
||||
<el-main>
|
||||
<!-- TODO: 日志和wiki链接 github链接 -->
|
||||
<el-input type="textarea" placeholder="请输入内容" v-model="paramsStr" disabled>
|
||||
<div class="log_form_father">
|
||||
<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-main>
|
||||
</el-main>
|
||||
@ -84,6 +103,7 @@
|
||||
<script src="../assets/scripts/vue.min.js"></script>
|
||||
<script src="../assets/scripts/element2.13.min.js"></script>
|
||||
<script>
|
||||
const vscode = acquireVsCodeApi()
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data() {
|
||||
@ -96,8 +116,13 @@
|
||||
}
|
||||
}
|
||||
return {
|
||||
showText:{
|
||||
oldParams: '', // 保存的参数
|
||||
paramsStr: '', // commit参数
|
||||
runNow: false, // 运行中
|
||||
logArr: [], // 保存的日志
|
||||
logStr: '', //
|
||||
},
|
||||
runNow: 0, // 0 初始化 1运行中 2运行结束 3取消运行
|
||||
form: {
|
||||
// itemSrc: '/Users/koro/work/web_my/testCommit',
|
||||
itemSrc: '点击选择要commit的项目文件夹',
|
||||
@ -139,7 +164,7 @@
|
||||
};
|
||||
},
|
||||
mounted() {
|
||||
// this.initListener();
|
||||
this.initListener();
|
||||
},
|
||||
methods: {
|
||||
submitForm(formName) {
|
||||
@ -154,13 +179,15 @@
|
||||
// 开始commit
|
||||
runCommit() {
|
||||
let params = this.form
|
||||
params.format = this.mapTimeData()
|
||||
this.runNow = true
|
||||
this.paramsStr = JSON.stringify(params)
|
||||
this.postMessage(params, 'commit');
|
||||
params.formatTime = this.mapTimeData()
|
||||
this.runNow = 1
|
||||
this.showText.paramsStr = JSON.stringify(params)
|
||||
this.showText.oldParams = this.showText.paramsStr
|
||||
// this.mockLog()
|
||||
this.postMessage('commit', params);
|
||||
},
|
||||
// 发送消息到插件
|
||||
postMessage(data, command) {
|
||||
postMessage(command, data) {
|
||||
vscode.postMessage({
|
||||
command,
|
||||
data
|
||||
@ -168,20 +195,55 @@
|
||||
},
|
||||
// 注册
|
||||
initListener() {
|
||||
// TODO: 通信问题 接收不到
|
||||
window.addEventListener('message', event => {
|
||||
console.log('event', event);
|
||||
const { command, data } = event.data;
|
||||
console.log('登录成功 addEventListener', event);
|
||||
if (command === 'success') {
|
||||
// 登录成功
|
||||
}
|
||||
console.log('data',data)
|
||||
console.log('event', event,command,data);
|
||||
// console.log('登录成功 addEventListener', event);
|
||||
// if (command === 'success') {
|
||||
// // 登录成功
|
||||
// }
|
||||
});
|
||||
},
|
||||
// 重置: commit成功和中断commit
|
||||
reset() {
|
||||
this.runNow = false;
|
||||
this.paramsStr = ''
|
||||
|
||||
// TODO: runNow 3 取消运行
|
||||
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) {
|
||||
@ -193,6 +255,9 @@
|
||||
return item.value
|
||||
})
|
||||
},
|
||||
chooseItem() {
|
||||
this.postMessage('choose-item')
|
||||
},
|
||||
// 增加时间范围选择
|
||||
addTimeRange() {
|
||||
this.form.timeArr.push({
|
||||
|
Loading…
Reference in New Issue
Block a user