Saturday, April 5, 2014

A Pub Package for Building a Single JS from Polymer.dart


Up tonight: extracting a command-line Dart script from my application into a Dart Pub package. Hopefully I made the script general enough last night.

Aside from missing generalizations, the challenge for this is how best to bundle the script in a pub package. Dart Pub is great for managing dependencies and smoke testing applications, but it does not quite have a good solution for scripts. Also, I have no idea how to write a script for Windows machines (but that's a task for another day). In my application, I have the script in the bin directory, which makes sense and is the Dart Pub standard location. The problem with that is that the bin directory of a package is not easily reached from an installed package.

In fact, the only thing that is easy to reach is the lib directory, so I start with that. I create a new git repository for my package, give it a reasonable starting point for a pubspec.yaml configuration, and create a lib directory to hold the script:
➜  repos  mkdir polymer-one-script
➜  repos  cd !$
➜  repos  cd polymer-one-script
➜  polymer-one-script  git init .
Initialized empty Git repository in /home/chris/repos/polymer-one-script/.git/
➜  polymer-one-script git:(master) mkdir lib
➜  polymer-one-script git:(master) cp ~/repos/polymer-book/play/deployment/dart/pubspec.yaml .
➜  polymer-one-script git:(master) ✗ cp ~/repos/polymer-book/play/deployment/dart/bin/reduce.dart lib/
➜  polymer-one-script git:(master) ✗ chmod 755 lib/reduce.dart 
I edit the pubspec.yaml of my new package:
name: polymer_one_script
dependencies:
  yaml: any
dev_dependencies:
  scheduled_test: any
With that, I am ready to remove last night's version of the script from my application and replace it with my new packaged script. I start by referencing the local package in my application's pubspec.yaml:
name: deployment_experiment
dependencies:
  polymer: any
dev_dependencies:
  scheduled_test: any
  polymer_one_script:
    path: /home/chris/repos/polymer-one-script
transformers:
- polymer:
    entry_points:
      - web/index.html
      - web/index2.html
Dart pub really is pretty awesome at packaging—including helping developers to build new packages. With the above path setting, pub build links to my local pub package directly where I am developing it:
➜  dart git:(master) ✗ ls -l packages/polymer_one_script 
lrwxrwxrwx 1 chris chris 40 Apr  5 21:59 packages/polymer_one_script -> /home/chris/repos/polymer-one-script/lib
I cannot run this packaged script directly from this location because the packages that it needs are not available in its installation location:
➜  dart git:(master) ✗ ./packages/polymer_one_script/reduce.dart
Unable to open file: /home/chris/repos/polymer-book/play/deployment/dart/packages/polymer_one_script/packages/yaml/yaml.dart'file:///home/chris/repos/polymer-book/play/deployment/dart/packages/polymer_one_script/reduce.dart': error: line 4 pos 1: library handler failed
import 'package:yaml/yaml.dart';
^
Instead, if I copy or link the packaged script into my application's bin directory, then it works:
➜  dart git:(master) ✗ ln -s packages/polymer_one_script/reduce.dart bin/
➜  dart git:(master) ✗ ./bin/reduce.dart
[web/index.html, web/index2.html]
And that does the trick. I'll upload a package to Pub shortly.

Update: and done.



Day #25

No comments:

Post a Comment