NuGet basics

The definitive documentation is nuget.org.
If you are a console person and don’t like nody UI stuff, you can use the Package Manager Console.

I am a NuGet user, most of the time, but once in a while, publish a package for my work and personal projects.

To prepare for package creation

Of course, download NuGet.exe, and make sure NuGet.exe is in your path.

Also, you need to register with nuget.org to get your api key, if you plan to share your package.

NuGet Update -self
NuGet SetApiKey Your-API-Key

Creating a package

If your package would simply contain a single assembly, you can create the package from an assembly. Otherwise, you would usually create it from a project, in which you can also benefit from tokenisation.

This creates a special nuspec file with tokens, to be replaced based on the project metaday.

Tokens

  • $id$: The Assembly name
  • $version$: The assembly version as specified in the assembly’s AssemblyVersionAttribute.
  • $author$: The company as specified in the AssemblyCompanyAttribute.
  • $description$: The description as specified in the AssemblyDescriptionAttribute.

In the folder where the csproj file, run:

nuget spec
nuget pack MyProject.csproj -Prop Configuration=Release

nuget spec will generate a spec file with basic metadata.

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2012</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
</package>

Once nuspec is ready, run nuget pack.
However, NuGet use the default build configuration set in the project file, and if you are a dev like me, it will be typically Debug. You wouldn’t want to publish your assembly built in Debug. so make sure to use Configuration option.

You would want to check the package you created to make sure everything necessary in there. Download NuGet Package Explorer and verify it.

Then publish it.

Add dependent assemblies to the package

Your project will have dependent assemblies (dlls), and want to include them in the package. If the dlls are references via NuGet, use . Otherwise, they are manually added, so use

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2012</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
        <dependency id="Hl.Local" version="[1.0.0.51]" />
        <dependency id="Hl.Valid" version="[1.0.0.50]" />
    </dependencies>
  </metadata>
  <files>
    <file src="..\lib\Hl.Frame.dll" target="lib\net35" />
  </files>

</package>

In dependency, if you use “[VERSION NO.]”, it will look for the exact version.

use a custom assemblyinfo from build server

You want to increase the version number from the build. Then you will need to update assemblyinfo before build the project. Then nuget package will have the verion number like Common.Framework.1.0.0.9.nupkg. Unfortunately, you have to use a custom build script, something like this. (it’s a psake script)

task BuildSolution -depends AssemblyInfo {
	Message("Building version $version")
	Exec { msbuild.exe "$slnFile" /t:Clean /t:Build /p:Configuration=$buildType /v:m /nologo }

task AssemblyInfo -depends Init {
	Update-AssemblyInfo -company "$COMPANY_NAME" -copyright "$COPYRIGHT" -LiteralPath "$baseDir\CustomAssemblyInfo.cs" -AssemblyVersion $script:version
}

NuGet basics

jQuery basics

This is the summary of “Fundamentals of Great jQuery Development”, which is available at http://vimeo.com/18511621.

javascript is a functional language, not in a useful way, but because it treats function as its first-class citizen. It is also Object-oriented language, as it treats everything as object.

function declaration
This is what people use most of the time.

function foo(p1) {
    p1();
}

function expression
This doesn’t have name and is anonymous.

foo(function () {});

function expression with name

foo(function f1(), "test" {});

json literals

var o = {
    foo: 1,
    bar: "test",
    alpha: {
        damian: function () { }
    }
};

Here o is an object.

constructor
“new” means you create a new object based on its protytype.
If you mean a class, capitalise the first letter as convention.

function Animal() {
    this.breed = "domestic tabby";
    this.smellsLike = "candy";
}
Animal.prototype = {
    member1: function() { },
    member2: ""
};

var f = new Animal(); // create a new animal based on the prototype.
f.member1
var f = Animal(); // this makes breed and smellsLike global scope, possibly overriding other breed.

Scope
In javascript, a variable’s scope is function-level. A variable is accessible within a function, even though it is declared within a block

function () {
    var i = 0;
    for (var j = 0; j <= 10; j++) {
        var x = 'test'; 
    }
    x = 20; //this x is visible outside of the for block
}
x = 20: // x is not visible outside of function

Global variable or function is bad, because you cannot guarantee that only your javascript would run in the browser. You have multiple javascript from 3rd party, grease-monkey plugins, …

So, don’t declare a function in the global scope. Instead, create anonymous function and immediately execute it.

(function() {
    var i = 'test'; //as long as you use 'var', it is safe.
    this.alert('hi'); // this is global window
    
}())

//If you want to pass window object
(function (w) {
    var i = 'test';
}(window))

this

function foo() {
    this //this is an global object
}

function Foo() {
    this // not an global object, but this function.
}
var f = new Foo();


//this can be resued depending on the context.
function foo() {
    alert(this.hi); //In this case, this becomes the function, not global.
}
foo.call({ hi: 'test' });
foo.call({ hi: 123 });

Closure
A closure is a function that references a variable that isn’t contained within its own immediate scope.

// this variable is out of the scope;
function foo() {
    var myVar = 1;

    return function() { 
        return myVar.toString();
    };
}

var myFunc = foo();
myFunc(); //This access myVar, which is outside of the function boundary. This looks trivial, but event handler takes advantage of closure.
jQuery basics

Rails Commands

Summary of various ruby commands

.erb: embedded ruby, the primary template system for including dynamic content in web pages.

Generation

$ rails generate integration_test static_pages

Undoing things

$ rails generate controller StaticPages home help
$ rails destroy  controller StaticPages home help

$ rails generate model Foo bar:string baz:integer
$ rails destroy  model Foo

$ rake db:migrate
$ rake db:rollback
$ rake db:migrate VERSION=0

Running tests

$ bundle exec rspec spec/requests/static_pages_spec.rb

Embedded Ruby template

<% provide(:title, 'About Us') %>
<!DOCTYPE html>
<html>
  <head>
    <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>

Unix commands

mv: rename

$ mv app/views/layouts/application.html.erb foobar

Git commands

Commit, merge, and push

$ git add .
$ git commit -m "Finish static pages"
$ git checkout master
$ git merge static-pages
$ git push

RVM and bundler integration

$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ cd ~/rails_projects/sample_app
$ bundle install --without production --binstubs=./bundler_stubs
Rails Commands

rails generate basics

create controller with help and home routes

$ rails generate controller StaticPages home help --no-test-framework

the option no-test-framework suppresses the default rspec test (or test::unit).

if you have created them by mistake, you can destroy them.

$ rails destroy controller StaticPages home help --no-test-framework

Creating a model is the same.

$ rails generate model Foo bar:string baz:integer
$ rails destroy model Foo

Errors

Could not find a javascript runtime.
Install node.js.

rails generate basics

rspec basics

When you create a new site, skip generating unit test, as you want to use rspec instead.

$ rails new sample_app --skip-test-unit

Add rspec to your gemfile

group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails', '2.9.0'
end

group :test do
  gem 'capybara', '1.1.2'
end

copybara gem allows you to interact with the app with a natual English-like syntaxt.

Then run bundle install to install those gems.

$ bundle install --without production

Configure your rails app to use rspec in place of test::unit.

$ rails generate rspec:install
rspec basics