Thursday, March 19, 2015

Simple fix for Module did not self-register

If you see the following error ...

Module did not self-register Error


[18:28:09] Error in plugin 'gulp-mocha'
Message:
    Module did not self-register.
Stack:
Error: Module did not self-register.
    at Error (native)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at bindings (/Users/lex/myproject/node_modules/jsdom/node_modules/contextify/node_modules/bindings/bindings.js:76:44)
    at Object. (/Users/lex/myproject/node_modules/jsdom/node_modules/contextify/lib/contextify.js:1:96)
    at Module._compile (module.js:460:26)
    at Module._extensions..js (module.js:478:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/lex/myproject/node_modules/6to5/lib/6to5/api/register/node.js:113:7)
[18:28:10] Finished 'scripts' after 21 s


Simple fix

... simply remove the node_modules directory and reinstall your npm modules.

/Users/lex/myproject/ $ rm -rf node_modules/
/Users/lex/myproject/ $ npm install


References

https://www.npmjs.com/


Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.

Recursively change file extension

Here's a technique you can use to recursively change the file extension of all files with a particular file extension, e.g., ".txt", to another one, e.g., ".js".

It leverages another technique discussed in the Process Array of Values in Bash Shell post.

It's a one liner, assuming you have a change-file-ext script.

One liner


$ for dir in "${dirs[@]}"; do (cd "$dir"; change-file-ext txt js)  ; done


change-file-ext script


#!/usr/bin/env ruby

# filename:  change-file-ext
# author:    Lex Sheehan
# copyright: Lex Sheehan LLC
# license:   GPL
# status:    production
# comments:  changes file extensions in current directory

def usage
  puts 'Usage:   change-file-ext  '
  puts 'Example: change-file-ext "css.erb" "css"'
  exit
end

if ARGV.size < 2 || ARGV.include?('--h')
  usage
else
  from_ext = "*.#{ARGV[0]}"
  to_ext = ARGV[1]
end

Dir.glob(from_ext).each{|i| `mv #{i} #{i.slice(0..-from_ext.size)}.#{to_ext}` }


References

http://lexsheehan.blogspot.com/2014/05/process-array-of-values-in-bash-shell.html


Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.

Thursday, February 12, 2015

Git Rebase

Rebase when you've been on a branch for a few days and you know there have been changes to master.

That way, when you do push your branch up for a pull request and code review it can be easily merged in with the master branch.

Assuming you're on my-feature-branch...

Rebase Commands


git checkout master
git pull
git checkout my-feature-branch
git rebase master
git push -f


Notes

Be sure to run git push -f, not what git is likely to suggest after you run the git rebase master command (git pull).

If you do have the misfortune of pulling changes into the rebase work you did, then you'll end up with a bunch of recent commits to the master branch, that you've already accounted for, in your pull request...

...and you will likely need to delete that branch and recreate it by cherry picking the commits you need.

References

http://git-scm.com/docs/git-rebase


Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.

Tuesday, February 10, 2015

fmt.Printf format reference

Here's the fmt.Printf format reference for Go (golang):

General

the value in a default format%v
type of the value%T

Integer

+255Always show sign%+d
'ÿ'Single quoted%q
377Octal without leading ‘0’%o
0377Octal with leading ‘0’%#o
ffHex lowercase%x
FFHex uppercase%X
0xffHex with “0x”%#x
U+00FFUnicode%U
U+00FF ‘ÿ’Unicode with char%#U
11111111Binary%b

Integer width

|    1|
|   12|
|  123|
| 1234|
|12345|
|123456|
|1234567|
|1    |
|12   |
|123  |
|1234 |
|12345|
|123456|
|1234567|
Minimum width=5. Expand if wider.%5d
%-5d (Left justify)
|    1|
|   12|
|  123|
| 1234|
|12345|
|12345|
|12345|
|1    |
|12   |
|123  |
|1234 |
|12345|
|12345|
|12345|
Width=5. Truncate right if wider.fmt.Printf(“%s”, fmt.Sprintf(“%5d”, value)[:5]))
fmt.Printf(“%s”, fmt.Sprintf(“%-5d”, value)[:5])) (Left justify)
  or
fmt.Printf(“|%5.5s|\n”, strconv.Itoa(value))
fmt.Printf(“|%-5.5s|\n”, strconv.Itoa(value)) (Left justify)
|    1|
|   12|
|  123|
| 1234|
|12345|
|23456|
|34567|
|1    |
|12   |
|123  |
|1234 |
|12345|
|23456|
|34567|
Width=5. Truncate left if wider.fmt.Printf(“%5d”, val % 100000)
fmt.Printf(“%-5d”, val % 100000) (Left justify)
|0012345|Pad with ‘0’ (Right justify only)%07d

Float

original%f (=%.6f)
6 digits after decimal
%e (=%.6e)
6 digits after decimal
%g
smallest number of digits necessary
%.3g
total number of digits=3
12345
1234.5
123.45
12.345
1.2345
0.12345
0.012345
0.0012345
0.00012345
0.000012345
0.0000012345
12345.000000
1234.500000
123.450000
12.345000
1.234500
0.123450
0.012345
0.001234
0.000123
0.000012
0.000001
1.234500e+04
1.234500e+03
1.234500e+02
1.234500e+01
1.234500e+00
1.234500e-01
1.234500e-02
1.234500e-03
1.234500e-04
1.234500e-05
1.234500e-06
12345
1234.5
123.45
12.345
1.2345
0.12345
0.012345
0.0012345
0.00012345
1.2345e-05
1.2345e-06
1.23e+04
1.23e+03
123
12.3
1.23
0.123
0.0123
0.00123
0.000123
1.23e-05
1.23e-06

String

love is "愛"Normal%s
"love is \"愛\""Double-quoted%q
`love is "愛"`Back-quoted if possible. Otherwise, double-quoted%#q
"love is \"\u611b\""Double-quoted. ASCII only.%+q
6c6f76652069732022e6849b22Hex lowercase.%x
6C6F76652069732022E6849B22Hex uppercase.%X
6c 6f 76 65 20 69 73 20 22 e6 84 9b 22Hex with space.% x

String width

|    1|
|   12|
|  123|
| 1234|
|12345|
|123456|
|1234567|
|12345678|
|123456789|
|1234567890|
|1    |
|12   |
|123  |
|1234 |
|12345|
|123456|
|1234567|
|12345678|
|123456789|
|1234567890|
Minimum width=5. Expand if wider.%5s
%-5s (Left justify)
|1|
|12|
|123|
|1234|
|12345|
|12345|
|12345|
Maximum width=5(Expand up to 5).%.5s
|    1|
|   12|
|  123|
| 1234|
|12345|
|123456|
|1234567|
|1234567|
|1234567|
|1234567|
|1    |
|12   |
|123  |
|1234 |
|12345|
|123456|
|1234567|
|1234567|
|1234567|
|1234567|
Minimum width=5. Expand up to 7.%5.7s
%-5.7 (Left justify)
|    1|
|   12|
|  123|
| 1234|
|12345|
|12345|
|12345|
|1    |
|12   |
|123  |
|1234 |
|12345|
|12345|
|12345|
Width=5.%5.5s
%-5.5s (Left justify)
|    1|
|   12|
|  123|
|  123|
|  123|
|1    |
|12   |
|123  |
|123  |
|123  |
Width=5. Truncate if wider than 3.%5.3s
%-5.3s (Left justify)
|0012345||12345  |Pad with ‘0’. Doen’t work when left-justify%07s

Struct

{sam {12345 67890}}Normal%v
{name:sam phone:{mobile:12345 office:67890}With field names%+v
main.People{name:”sam”, phone:main.Phone{mobile:”12345”, office:”67890”}}Go’s syntax%#v

Boolean

true
false
Boolean%t

Pointer

0x1842c220Pointer with 0x%p
1842c220Pointer without 0x%#p

References

http://golang-examples.tumblr.com/post/86795367134/fmt-printf-format-reference-cheat-sheet


Share this article


This work is licensed under the Creative Commons Attribution 3.0 Unported License.

Monday, February 2, 2015

Running out of RAM on Yosemite

If you notice that you're running out of RAM on OSX Yosimite...

First, open you Activity Monitor, click the [Memory] tab and sort by "Memory"

If WindowServer is hogging your RAM then resetting you mac's PRAM might fix things.

Reset your mac's PRAM

  • Close all open apps
  • Press CMD + Option + ESC (***)
  • Put your mac to Sleep
  • Wake you mac
  • Restart your mac
  • Press CMD + Option + P + R (keep it down until you hear at least 2 chimes)

(***) Here's where you force quit anything left open. It's okay to leave Finder open

Notes

If you also have excessive CPU utilization, you may want to Repair Disk Permissions, which adds a step to the above process:

Reset Disk Permissions and your mac's PRAM

  • Close all open apps
  • Press CMD + Option + ESC (***)
  • Put your mac to Sleep
  • Wake you mac
  • Restart your mac
  • click “Repair disk permissions”
  • Restart your mac
  • Press CMD + Option + P + R (keep it down until you hear at least 2 chimes)

References

http://support.apple.com/en-us/HT201560
https://discussions.apple.com/thread/6623697


Share this article

profile for l3x at Stack Overflow, Q&A for professional and enthusiast programmers


This work is licensed under the Creative Commons Attribution 3.0 Unported License.

Friday, January 30, 2015

Print Golang Stacktrace

Some IDEs are good at letting you drill down into function definitions.

That's great when you know the starting point.

What about the times when you know exactly where your code broke and you want to find the starting point, as well as all the stops along the way?

Here's one way to do it using runtime/pprof

Output Stack Trace

Add Imports


import (
 "runtime/pprof"
 "os"
)


Generate Stack Trace

Put this on what ever line you want to generate the stack trace from:

pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)


In this example, I put that WriteTo statement on line 222 in file /Users/lex/dev/go/project/src/github.com/company/repo/server/session.go.

Ouput

The output will look something like this:

goroutine profile: total 9
1 @ 0x18ce8c 0x18cc3b 0x189be3 0x3c807 0x3c529 0x3cfe2 0x3ceb1 0x3ce57 0x2cab2 0x1a0060 0x19ed07 0x25c437 0x15af96 0x15aea2 0x16b1f0 0x2cb12 0x1a0060 0x19ed07 0x25c437 0x15af96 0x15aea2 0x15f206 0x2cab2 0x1a0060 0x19ed07 0x25c437 0x15af96 0x15aea2 0x15ea7e 0x2cb12 0x1a0060 0x19ed07
# 0x18ce8c runtime/pprof.writeRuntimeProfile+0xcc        /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/pprof/pprof.go:540
# 0x18cc3b runtime/pprof.writeGoroutine+0x9b        /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/pprof/pprof.go:502
# 0x189be3 runtime/pprof.(*Profile).WriteTo+0xd3        /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/pprof/pprof.go:229
# 0x3c807  github.com/company/repo/server.(*sessionContext).User+0x1f7   /Users/lex/dev/go/project/src/github.com/company/repo/server/session.go:222
# 0x3c529  github.com/company/repo/server.(*sessionContext).UserID+0x39   /Users/lex/dev/go/project/src/github.com/company/repo/server/session.go:193
# 0x3cfe2  github.com/company/repo/server.(*sessionContext).refreshSessionRenderVars+0x42 /Users/lex/dev/go/project/src/github.com/company/repo/server/session.go:285
# 0x3ceb1  github.com/company/repo/server.(*sessionContext).SetSessionRenderVars+0x51 /Users/lex/dev/go/project/src/github.com/company/repo/server/session.go:274
# 0x3ce57  github.com/company/repo/server.SetSessionRenderVars+0x37   /Users/lex/dev/go/project/src/github.com/company/repo/server/session.go:269
# 0x2cab2  runtime.call32+0x32          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/asm_amd64.s:361
# 0x1a0060 reflect.Value.call+0x1210         /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:563
# 0x19ed07 reflect.Value.Call+0xd7          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:411
# 0x25c437 github.com/codegangsta/inject.(*injector).Invoke+0x3b7      /Users/lex/dev/go/project/src/github.com/codegangsta/inject/inject.go:102
# 0x15af96 github.com/company/martini.(*context).run+0x86      /Users/lex/dev/go/project/src/github.com/company/martini/martini.go:163
# 0x15aea2 github.com/company/martini.(*context).Next+0x32      /Users/lex/dev/go/project/src/github.com/company/martini/martini.go:154
# 0x16b1f0 github.com/company/martini-sessions.func·002+0x230      /Users/lex/dev/go/project/src/github.com/company/martini-sessions/sessions.go:93
# 0x2cb12  runtime.call64+0x32          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/asm_amd64.s:362
# 0x1a0060 reflect.Value.call+0x1210         /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:563
# 0x19ed07 reflect.Value.Call+0xd7          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:411
# 0x25c437 github.com/codegangsta/inject.(*injector).Invoke+0x3b7      /Users/lex/dev/go/project/src/github.com/codegangsta/inject/inject.go:102
# 0x15af96 github.com/company/martini.(*context).run+0x86      /Users/lex/dev/go/project/src/github.com/company/martini/martini.go:163
# 0x15aea2 github.com/company/martini.(*context).Next+0x32      /Users/lex/dev/go/project/src/github.com/company/martini/martini.go:154
# 0x15f206 github.com/company/martini.func·004+0x76       /Users/lex/dev/go/project/src/github.com/company/martini/recovery.go:140
# 0x2cab2  runtime.call32+0x32          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/asm_amd64.s:361
# 0x1a0060 reflect.Value.call+0x1210         /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:563
# 0x19ed07 reflect.Value.Call+0xd7          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:411
# 0x25c437 github.com/codegangsta/inject.(*injector).Invoke+0x3b7      /Users/lex/dev/go/project/src/github.com/codegangsta/inject/inject.go:102
# 0x15af96 github.com/company/martini.(*context).run+0x86      /Users/lex/dev/go/project/src/github.com/company/martini/martini.go:163
# 0x15aea2 github.com/company/martini.(*context).Next+0x32      /Users/lex/dev/go/project/src/github.com/company/martini/martini.go:154
# 0x15ea7e github.com/company/martini.func·001+0x1ae       /Users/lex/dev/go/project/src/github.com/company/martini/logger.go:16
# 0x2cb12  runtime.call64+0x32          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/asm_amd64.s:362
# 0x1a0060 reflect.Value.call+0x1210         /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:563
# 0x19ed07 reflect.Value.Call+0xd7          /usr/local/Cellar/go/1.3.3/libexec/src/pkg/reflect/value.go:411

. . .


Notes

There will be several sections of traces.

You'll probably only be interested in the first one.

References

http://golang.org/pkg/runtime/debug/#PrintStack


Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.The output will look something like this:

js-pre-commit-git-hook && go-pre-commit-git-hook


Notes

Put the scripts in your $PATH and make them executable.

Create a .jshintrc file with your desired jshint configuration settings.

References



Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.

Tuesday, December 23, 2014

Pre Commit Hook for JS and Go

If you have a web project that has both JavaScript and Go, then you may need to create a pre-commit hook to validate both .js and .go source files.

Here's one way to do it using two script files and a git pre-commit hook.

Create pre commit scripts for JS and Go

js-pre-commit-git-hook


files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" = "" ]; then
    exit 0
fi
LINTER=jshint
pass=true

echo "\nValidating JavaScript:\n"

for file in ${files}; do
    result=$($LINTER ${file})
    if [ "$?" == "0" ]; then
        echo "\t\033[32m$LINTER Passed: ${file}\033[0m"
    else
        echo "\t\033[31m$LINTER Failed: ${file}\033[0m"
        pass=false
    fi
done

echo "\nJavaScript validation complete\n"

if ! $pass; then
    echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass $LINTER but do not. Please fix the $LINTER errors and try again.\n"
    exit 1
else
    echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi



go-pre-commit-git-hook


#!/bin/sh
# Copyright 2012 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# git gofmt pre-commit hook
#
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
#
# This script does not handle file names that contain spaces.

gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
[ -z "$gofiles" ] && exit 0

unformatted=$(gofmt -l $gofiles)
[ -z "$unformatted" ] && exit 0

# Some files are not gofmt'd. Print message and fail.

echo >&2 "Go files must be formatted with gofmt. Please run:"
for fn in $unformatted; do
 echo >&2 "  gofmt -w $PWD/$fn"
done

exit 1


.git/hooks/pre-commit


js-pre-commit-git-hook && go-pre-commit-git-hook


Notes

Put the scripts in your $PATH and make them executable.

Create a .jshintrc file with your desired jshint configuration settings.

References



Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.