From 54b79a2ed3a2e74bb846a41935bbf273e251698d Mon Sep 17 00:00:00 2001 From: OBKoro1 <1677593011@qq.com> Date: Wed, 1 Jan 2020 17:37:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BB=A5=E5=8F=8A=E5=AE=8C=E6=88=90=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- src/models/commitHandle.ts | 159 ++++++++++++++++++++++++------------- src/models/index.ts | 7 +- src/views/autoCommit.html | 112 ++++++++++++++++---------- 4 files changed, 182 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index 45adc74..5259f24 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # autoCommit -# 用于学习交流 \ No newline at end of file +# 用于学习交流 + + + \ No newline at end of file diff --git a/src/models/commitHandle.ts b/src/models/commitHandle.ts index 26a8fbe..cd12f58 100644 --- a/src/models/commitHandle.ts +++ b/src/models/commitHandle.ts @@ -2,7 +2,7 @@ * Author : OBKoro1 * Date : 2019-12-30 16:59:30 * LastEditors : OBKoro1 - * LastEditTime : 2019-12-31 18:00:43 + * LastEditTime : 2020-01-01 17:32:41 * FilePath : /autoCommit/src/models/commitHandle.ts * Description : commit 具体操作 * https://github.com/OBKoro1 @@ -13,99 +13,144 @@ 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 { getPanelWebview, outputLog, isProduction } from '../util/vscodeUtil'; import WebView from './WebView'; +interface timeElement { + value: Array; + commitNumber: number; +} + +interface dayTime { + value: string; + commitNumber: number; +} + class CommitHandle { public paramsObj: any; - public timeArr: Array; + public timeArr: Array; public autoCommitView: WebView; - + private userCancel: boolean; constructor(message: webviewMsg) { this.paramsObj = message.data; this.timeArr = []; this.timeHandle(); this.autoCommitView = getPanelWebview(); + this.userCancel = false; } // 处理所有时间段 timeHandle() { // 处理所有时间范围 - this.paramsObj.formatTime.forEach((item: Array) => { + this.paramsObj.timeArr.forEach((item: timeElement) => { // 获取每个时间范围的具体日期 - let detailTimeArr = this.getAllDay(item[0], item[1]); - // 日期去重 - detailTimeArr = detailTimeArr.filter(ele => { - return !this.timeArr.includes(ele); + let detailTimeArr = this.getAllDay(item.value[0], item.value[1]); + // 日期去重 组织数据 + detailTimeArr.forEach(ele => { + let index = this.timeArr.findIndex(element => { + return element.value === ele; + }); + // 添加不存在的日期 + if (index === -1) { + this.timeArr.push({ + value: ele, + commitNumber: item.commitNumber + }); + } }); - // TODO: commit number - // if(){ - - // } - // detailTimeArr.map(element=>{ - // return { - // time: element - // } - // }) - this.timeArr.push(...detailTimeArr); }); this.sortTime(); } // 日期排序 sortTime() { - this.timeArr = this.timeArr.sort((item1: string, item2: string): number => { - const dateArr1: Array = item1.split('-'); - const dateArr2: Array = item2.split('-'); - if (dateArr1[0] === dateArr2[0]) { - if (dateArr1[1] === dateArr2[1]) { - // 日期不同就比较日期 - return dateArr1[2] - dateArr2[2]; + this.timeArr = this.timeArr.sort( + (item1: dayTime, item2: dayTime): number => { + const dateArr1: Array = item1.value.split('-'); + const dateArr2: Array = item2.value.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[1] - dateArr2[1]; + // 年份不同就比较年份 + return dateArr1[0] - dateArr2[0]; } - } else { - // 年份不同就比较年份 - return dateArr1[0] - dateArr2[0]; } - }); + ); this.commitFn(); } - // TODO: 中断 - commitFn() { - outputLog('将要commit的日期:', this.timeArr); - outputLog('每个日期提交次数:', this.paramsObj.commitNumber); + async commitFn() { + outputLog('将要commit的日期:', JSON.stringify(this.timeArr)); let totalNum = 0; // 总commit次数 // 遍历日期 - this.timeArr.forEach(item => { + for (let item of this.timeArr.values()) { + if (this.cancelCommit()) break; // 每个日期commit次数 - for (let i = 0; i < this.paramsObj.commitNumber; i++) { - let time = this.formatTime(item); // 2019-01-02 08:00 + let dayCommitNumber = this.paramsObj.commitNumber; + if (item.commitNumber !== 0) { + // 如果该范围有commit次数 则用该范围的 + dayCommitNumber = item.commitNumber; + } + for (let i = 0; i < dayCommitNumber; i++) { + if (this.cancelCommit()) break; + let time = this.formatTime(item.value); // 2019-01-02 08:00 time = moment(time).format(); // 2019-01-02T00:00:00+0800 - const commitContent = `${time} \n 随机数:${RandomNumber(1, 100000)}`; + const commitContent = `${time} \n随机数:${RandomNumber( + 1, + 100000 + )}\n提交次数:${totalNum + 1}`; fs.writeFileSync( - `${this.paramsObj.itemSrc}/${this.paramsObj.fileSrc}`, + `${this.paramsObj.itemSrc}/${this.paramsObj.fileName}`, 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); + const isDebug = true; // 手动更改调试模拟是否提交git + if (!isProduction() && !isDebug) { + // TODO: 测试提交 以及接受log + 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); + } else { + // 模拟git提交 + const test = await new Promise((resolve, reject) => { + setTimeout(() => { + outputLog('延迟一秒'); + resolve('延迟一秒'); + }, 1000); + }); + } totalNum++; outputLog('commit内容', commitContent); } - outputLog(`总commit次数${totalNum}`); - outputLog('自动commit完成'); - }); + } + this.commitEnd(totalNum); + } + commitEnd(totalNum: number) { + this.userCancel = false; // 重新打开终止开关 + this.autoCommitView.postMessage('commit 完成', 'commit 完成') + outputLog('自动commit完成', `总commit次数${totalNum}`); + } + cancelCommit() { + if (this.userCancel) { + outputLog('终止自动commit'); + } + return this.userCancel; + } + public closeCommit() { + this.userCancel = true; } // 格式化日期 formatTime(time: string) { diff --git a/src/models/index.ts b/src/models/index.ts index f2e4be2..dc791b8 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -2,7 +2,7 @@ * Author : OBKoro1 * Date : 2019-12-25 17:08:18 * LastEditors : OBKoro1 - * LastEditTime : 2019-12-31 16:42:46 + * LastEditTime : 2020-01-01 17:20:11 * FilePath : /autoCommit/src/models/index.ts * Description : 插件逻辑入口 * https://github.com/OBKoro1 @@ -20,7 +20,6 @@ class ExtensionLogic { public MessageCallBack: any; public autoCommitView: WebView; public CommitHandle: any; - public isDebug: boolean; public constructor(context: vscode.ExtensionContext) { this.context = context; @@ -28,7 +27,6 @@ class ExtensionLogic { this.context, this.messageCallBack.bind(this) ); - this.isDebug = true; setPanelWebview(this.autoCommitView); this.createView(); } @@ -47,8 +45,11 @@ class ExtensionLogic { this.CommitHandle = new CommitHandle(message); } else if (message.command === 'choose-item') { this.publishChooseFile(); + } else if(message.command === 'cancel autoCommit'){ + this.CommitHandle.closeCommit() } } + // 选择项目文件夹 async publishChooseFile() { const urlArr: any = await vscode.window.showOpenDialog({ canSelectFiles: false, // 允许选择文件 diff --git a/src/views/autoCommit.html b/src/views/autoCommit.html index d0af5a8..91e420d 100644 --- a/src/views/autoCommit.html +++ b/src/views/autoCommit.html @@ -13,10 +13,9 @@
Github自动提交commit工具
-
{{ event }}
- + - + - - 删除 + + + + + 删除 - 开始commit - 新增时间范围 - 取消commit - 清空日志 + 开始commit + 新增日期范围 + 取消commit + 清空日志
日志和文档
- + + - + + - + Readme 更新日志 -
觉得这个插件还不错的话, 请给我点个Star⭐️吧~
+
觉得这个插件还不错的话, 请给我点个Star⭐️吧~
@@ -104,7 +120,13 @@ \ No newline at end of file