Sunday, October 21, 2012

Typescript preview on AppHarbor

Hola!


So, this weekend a friend and I started working on a little side project and wanted to use Microsoft's new TypeScript language for our JS development.  We had also decided on using AppHarbor (a fantastic .net paas provider) to automatically deploy our code builds.  There were a few oddities that had to be fixed to allow this to work, and I hope to share with you the process that we went through to allow for typescript to be successfully deployed to an ASP.NET MVC app running on AppHarbor.

Installation

  1. Install VS 2012 (For my case I used Professional 2012)
  2. Install the Typescript Preview (version 0.8.0.0)
  3. Install the Visual Studio plugin if it didn't get successfully installed
    • Manually run the file TypeScriptLanguageService.vsix found at   %PROGRAM_FILES%\Microsoft SDKs\TypeScript\0.8.0.0\

Project Setup

At this point we now have visual studio and a developer box setup to create our new project.  The project template that I used was an ASP.NET MVC 4 web application.  After creating the project file you will need to edit the .csproj file to have some tags to run the tsc (TypeScript compiler) on the ts files found in your project.  We will also need to copy the tsc compiler to our solution, so that we can commit it to the repository (required only for AppHarbor building).

  1. Create a folder in a subdirectory of your solution and copy the following files from the TypeScript install directory
    • tsc.exe
    • tschost.dll (not sure if it is actually required)
    • tsc.js
  2. Noting the above directories location relative to the solution's root, you will then edit the projects csproj file using your favorite editor and add the below lines
  <Target Name="BeforeBuild">
    <Exec Command="&quot;$(SolutionDir)<TypescriptDirectory>\tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
  </Target>

This new command will cause the typescript compiler to run before each build  and compile the .ts files to .js
Note: The reason for copying the compiler over is so that it will be able to be executed on the AppHarbor machines without the need for them to install the dependency. 

Finishing

So, at this point you should have an application that will build your typescript files on any machine that can build a regular csproj project.  When adding the ts file to your project, use the visual studio "Add New Item" option and select a typescript file, if you try to manually add it with the extension you will need to add the compile command to the projects csproj file as well (example below).

     <TypeScriptCompile Include="TSScripts\app.ts" />

Wednesday, May 9, 2012

Installing latest django on server with no root access

So I am going to talk about what I needed to do get the lastest django (1.4) working on site5.
1. Lets setup our little sandbox area for placing the source to compile and the actually compiled instance
cd ~
mkdir installed
cd installed
mkdir compile
cd compile
2. Download Python 2.7 (Currently django doesn't work with python 3)
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar -xvf Python-2.7.3.tar.bz2

Note: This is specific for version 2.7.3 which was the stable release at the time this article was written, but in theory this should work with any future releases as well.
3. Compiling and install Python 2.7 (non root user)
cd Python-2.7.3
./configure PREFIX=
make install DESTDIR=/home/<username>/installed/python2.7
4. Add python to the PATH for simplicity, to do this we will add an entry to the .bash_profile file located in our home directory
cd ~
vim .bash_profile

add $HOME/installed/pythong2.7/bin to the PATH defined in .bash_profile
PATH=$HOME/installed/python2.7/bin:$PATH
export PATH
5. Reload the .bash_profile so that python will be on the path.
source .bash_profile
6. Verify that the new python is found in the path
python --version
(should print Python 2.7.3)
7. Install PIP (I followed instructions found here)
cd ~/installed/compile
curl http://python-distribute.org/distribute_setup.py | python
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

*Note if the last command fails with certificate issue, run with curl -k
8. Install django using pip
pip install django
pip install flup
pip install pysqlite  (only sometimes required and only if you are doing sqlite db)
9. Create directory for website and add necessary files to get apache to pick it up
.htaccess
AddHandler fcgid-script .fcgi
Options +FollowSymLinks

RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(static/.*)$ - [L]
RewriteCond %{REQUEST_URI} !(django.fcgi)
RewriteRule ^(.*)$ django.fcgi/$1 [L]
django.fcgi
#!/home/<username>/installed/python2.7/bin/python
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home/<username>/<website-location>")

# Switch to the directory of your project. (Optional.)
#os.chdir("/home/<username>/<website-location>")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "<projectname>.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
So hopefully after following this little tutorial, you too can get the newest version of django up and running on a hosted machine with no root access ;-)