Skip to content

Commit

Permalink
release v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ZimoLoveShuang committed Nov 23, 2020
0 parents commit 29ee826
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Wakatime For HBuilderX

开发文档参考:[https://github.com/dcloudio/hbuilderx-extension-docs](https://github.com/dcloudio/hbuilderx-extension-docs)

开发文档参考:[https://wakatime.com/help/creating-plugin#getting-started](https://wakatime.com/help/creating-plugin#getting-started)

# What's this?

为hbuilderx写的wakatime插件

# How to use this?

## online

直接在[插件商店](https://ext.dcloud.net.cn/)搜索wakatime,安装即可

## offline

1. 下载[release](https://github.com/ZimoLoveShuang/wakatime-hbuilderx/releases)中的`wakatime-hbuilderx.zip`
2. 解压到`hbuilderx的安装目录/plugins`
3. 重启hbuilderx

## 源码安装

1. clone 此项目,`git clone https://github.com/ZimoLoveShuang/wakatime-hbuilderx`
2. 安装,`nodejs`
3. 安装依赖,`npm install`
3. 将整个项目复制到`hbuilderx的安装目录/plugins`
4. 重启hbuilderx

# 更新日志

- 2020-11-23 发布 v1.0.0

133 changes: 133 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const hx = require("hbuilderx");

const os = require('os')
const fs = require('fs')
const path = require('path')
const ini = require('ini')
const request = require('request')

const plugin_name = 'wakatime-hbuilderx'
const plugin_version = '1.0.0'
const ide = hx.env.appName
const ide_version = hx.env.appVersion
const config_path = path.format({
dir: os.homedir(),
base: '.wakatime.cfg'
})

var lastAction = 0,
lastFile = undefined;

// 读取api_key,如果不存在就创建
function read_api_key() {
// console.log(config_path)
try {
config = ini.parse(fs.readFileSync(config_path, 'utf-8'))
return config.settings.api_key
} catch (e) {
// 没有配置api_key
hx.window.showInputBox({
prompt: '请输入api_key'
}).then((result) => {
var partten = /\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/
if (partten.test(result)) {
var config = {
settings: {
api_key: ''
}
}
config.settings.api_key = result
fs.writeFileSync(config_path, ini.stringify(config))
console.log('the input api_key is ' + result)
return result
}
return read_api_key()
})
}
}


function enoughTimePassed() {
return lastAction + 120000 < Date.now();
}

// 发送心跳包
function sendHeartbeat(file, time, project, language, isWrite, lines, api_key) {
var data = {
entity: file,
type: 'file',
category: 'coding',
time: time / 1000,
project: project,
language: language,
lines: lines,
is_write: isWrite ? true : false,
plugin: plugin_name + '/' + plugin_version,
}

// console.log(JSON.stringify(data))
request({
url: 'https://api.wakatime.com/api/v1/users/current/heartbeats',
method: 'POST',
json: true,
headers: {
'content-type': 'application/json',
'Authorization': 'Basic ' + Buffer.from(api_key).toString('base64')
},
body: data
}, function(error, response, body) {
if (!error && response.statusCode == 201) {
console.log('send heartbeat success.')
}
})

lastAction = time
lastFile = file
}

function handleAction(isWrite, api_key) {
hx.window.getActiveTextEditor()
.then(function(editor) {
var currentDocument = editor.document
// console.log(currentDocument)
var time = Date.now()
if (isWrite || enoughTimePassed() || lastFile !== currentDocument.uri.fsPath) {
hx.workspace.getWorkspaceFolder("%fsPath%").then(function(wsFolder) {
var language = currentDocument.languageId ? currentDocument.languageId : undefined
var project = wsFolder.name ? wsFolder.name : undefined
var editor = ide
var lines = currentDocument.lineCount ? currentDocument.lineCount : undefined
sendHeartbeat(currentDocument.uri.fsPath, time, project, language, isWrite, lines, api_key)
})
}
})
}

//该方法将在插件激活的时候调用
function activate(context) {
console.log('wakatime-hbuilderx started.')
console.log('wakatime init.')
console.log('check api_key')
var api_key = read_api_key()
console.log('the api_key is ' + api_key)
console.log('binding to ide events.')
hx.workspace.onDidChangeTextDocument(function(event) {
let document = event.document;
// console.log('文档被修改时的事件' + JSON.stringify(document))
handleAction(false, api_key)
})
hx.workspace.onDidSaveTextDocument(function(document) {
// console.log('文档被保存时的事件' + JSON.stringify(document))
handleAction(true, api_key)
})
}

//该方法将在插件禁用的时候调用(目前是在插件卸载的时候触发)
function deactivate() {
console.log('wakatime-hbuilderx stoped.')
}

module.exports = {
activate,
deactivate
}
Loading

0 comments on commit 29ee826

Please sign in to comment.