Skip to content

Commit

Permalink
Merge pull request #59 from wakatime/use-new-go-cli
Browse files Browse the repository at this point in the history
Use new Go wakatime-cli
  • Loading branch information
alanhamlett authored Nov 4, 2021
2 parents 8dfe67c + 05fc33a commit 0f94ac2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 67 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

## Installation

Heads Up! WakaTime depends on [Python](http://www.python.org/getit/) being installed to work correctly.

1. Install wakatime-mode for Emacs using [MELPA](https://melpa.org/#/wakatime-mode) (Doom users see [these instructions][doom install] instead).

2. Install [wakatime-cli](https://pypi.python.org/pypi/wakatime) with `pip install wakatime`.
2. Download [wakatime-cli](https://github.com/wakatime/wakatime-cli/releases) to `~/.wakatime/` or somewhere in your `$PATH`. (Or `brew install wakatime-cli` on Mac)

3. Add `(global-wakatime-mode)` to your `init.el` file, then restart Emacs.

4. You will see a prompt asking for the path to wakatime-cli. Run `which wakatime` and enter that path into the emacs prompt, then press `enter`.
4. You will see a prompt asking for the path to wakatime-cli. Run `which wakatime-cli` and enter that path into the emacs prompt, then press `enter`.

5. Enter your [api key](https://wakatime.com/settings#apikey) in your `init.el` or `~/.wakatime.cfg` file.
5. Enter your [api key](https://wakatime.com//api-key) in your `init.el` or `~/.wakatime.cfg` file.

6. Use Emacs with wakatime-mode turned on and your time will be tracked for you automatically.

Expand All @@ -34,11 +32,9 @@ Enable WakaTime for the current buffer by invoking `M-x wakatime-mode`. If you

## Configuration

Set variable `wakatime-api-key` to your [API key](https://wakatime.com/#apikey).

Point `wakatime-cli-path` to the absolute path of [wakatime-cli](https://pypi.python.org/pypi/wakatime).
Set variable `wakatime-api-key` to your [API key](https://wakatime.com/api-key).

Optionally, point `wakatime-python-bin` to the absolute path of python on your system. Defaults to `python` which only works if python is in your PATH.
Point `wakatime-cli-path` to the absolute path of [wakatime-cli](https://github.com/wakatime/wakatime-cli/releases).


## Troubleshooting
Expand Down
65 changes: 7 additions & 58 deletions wakatime-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
;; `global-wakatime-mode'.

;; Set variable `wakatime-api-key' to your API key. Point
;; `wakatime-cli-path' to the absolute path of the CLI script
;; (wakatime-cli.py).
;; `wakatime-cli-path' to the absolute path of the CLI binary available
;; from <https://github.com/wakatime/wakatime-cli/releases>.

;;; Code:

Expand All @@ -38,7 +38,6 @@
(defvar wakatime-noprompt nil)
(defvar wakatime-init-started nil)
(defvar wakatime-init-finished nil)
(defvar wakatime-python-path nil)

(defgroup wakatime nil
"Customizations for WakaTime"
Expand All @@ -55,11 +54,6 @@
:type 'string
:group 'wakatime)

(defcustom wakatime-python-bin "python"
"Path of Python binary."
:type 'string
:group 'wakatime)

(defcustom wakatime-disable-on-error nil
"Turn off wakatime-mode and wakatime-global-mode when errors in
the wakatime subprocess occurs."
Expand All @@ -76,41 +70,14 @@ the wakatime subprocess occurs."
(or (null string)
(zerop (length string))))

(defun wakatime-guess-actual-script-path (path)
(let ((true-path (if (null path) nil (file-truename path))))
(let ((true-path (if (null true-path) path true-path)))
(cond
((s-blank true-path) ; just return nil if path is null
nil)
((string-match-p "\\.pyenv" true-path) ; pyenv
(with-temp-buffer
(call-process "pyenv" nil t nil "which" "wakatime")
(delete-char -1) ; delete newline at the end of output
(buffer-string)))
((string-match-p "Cellar" true-path) ; Homebrew
(let* ((libexec (format "%slibexec/" (file-name-directory (directory-file-name (file-name-directory true-path)))))
(python-path (format "%slib/python2.7/site-packages" libexec)))
(setq wakatime-python-path python-path)
(format "%sbin/wakatime" libexec)))
((file-exists-p (format "%s/cli.py" true-path)) ; point to cli.py from source code
(format "%s/cli.py" true-path))
((file-exists-p (format "%s/wakatime/cli.py" true-path)) ; point to cli.py from source code inside wakatime package folder
(format "%s/wakatime/cli.py" true-path))
(t path)))))

(defun wakatime-init ()
(unless wakatime-init-started
(setq wakatime-init-started t)
(when (s-blank wakatime-cli-path)
(customize-set-variable 'wakatime-cli-path
(wakatime-guess-actual-script-path (wakatime-find-binary "wakatime"))))
(wakatime-find-binary "wakatime-cli")))
(when (s-blank wakatime-cli-path)
(wakatime-prompt-cli-path))
(when (not (s-blank wakatime-cli-path))
(if (not (string-match-p "cli\\.py$" wakatime-cli-path))
(customize-set-variable 'wakatime-python-bin nil)
(when (s-blank wakatime-python-bin)
(wakatime-prompt-python-bin))))
(setq wakatime-init-finished t)))

(defun wakatime-prompt-api-key ()
Expand All @@ -123,30 +90,14 @@ the wakatime subprocess occurs."
(setq wakatime-noprompt nil)))

(defun wakatime-prompt-cli-path ()
"Prompt user for cli.py path."
"Prompt user for wakatime-cli binary path."
(when (and (= (recursion-depth) 0) (not wakatime-noprompt))
(setq wakatime-noprompt t)
(let ((cli-path (read-string "WakaTime CLI script path (wakatime/cli.py): ")))
(let ((cli-path (read-string "WakaTime CLI binary path: ")))
(customize-set-variable 'wakatime-cli-path cli-path)
(customize-save-customized))
(setq wakatime-noprompt nil)))

(defun wakatime-prompt-python-bin ()
"Prompt user for path to python binary."
(when (and (= (recursion-depth) 0) (not wakatime-noprompt))
(setq wakatime-noprompt t)
(let ((python-bin (read-string "Path to python binary: ")))
(customize-set-variable 'wakatime-python-bin python-bin)
(customize-save-customized)
)
(setq wakatime-noprompt nil)
)
nil)

(defun wakatime-python-exists (location)
"Check if python exists in the specified path location."
(= (condition-case nil (call-process location nil nil nil "--version") (error 1)) 0))

(defun wakatime-find-binary (program)
"Find the full path to an executable program."
(cond
Expand All @@ -169,9 +120,8 @@ the wakatime subprocess occurs."
(defun wakatime-client-command (savep)
"Return client command executable and arguments.
Set SAVEP to non-nil for write action."
(format "%s%s--entity \"%s\" --plugin \"%s/%s\" --time %.2f%s%s"
(if (s-blank wakatime-python-bin) "" (format "\"%s\" " wakatime-python-bin))
(if (s-blank wakatime-cli-path) "wakatime " (format "\"%s\" " wakatime-cli-path))
(format "%s--entity \"%s\" --plugin \"%s/%s\" --time %.2f%s%s"
(if (s-blank wakatime-cli-path) "wakatime-cli " (format "\"%s\" " wakatime-cli-path))
(buffer-file-name (current-buffer))
wakatime-user-agent
wakatime-version
Expand All @@ -184,7 +134,6 @@ the wakatime subprocess occurs."
(let*
(
(command (wakatime-client-command savep))
(process-environment (if wakatime-python-path (cons (format "PYTHONPATH=%s" wakatime-python-path) process-environment) process-environment))
(process
(start-process
"Shell"
Expand Down

0 comments on commit 0f94ac2

Please sign in to comment.