Code Monkey home page Code Monkey logo

trypurescript's Introduction

Try PureScript

Build Status

Try PureScript is an online PureScript code editor for quickly experimenting with PureScript code snippets and ideas. It consists of a client and a server component, both of which live within this repository.

Features:

  • Writing code using the Ace Editor
  • Automatic compilation
  • PureScript syntax highlighting
  • Run and print output or show resulting JavaScript
  • Multiple view modes: code, output or both
  • Shareable code and editor state via URL
  • Load PureScript code from GitHub Gists or repository files

Control Features via the Query String

Most of these features can be controlled not only from the toolbar, but also using the query parameters:

  • Load From GitHub Repo: Load a PureScript file from a GitHub repository using the github parameter

    • Format: github=/<owner>/<repo>/<branch or tag>/<file>.purs
    • Example: github=/purescript/trypurescript/master/client/examples/Main.purs.
    • Notes: the file should be a single PureScript module with the module name Main.
  • Load From Gist: Load PureScript code from a gist using the gist parameter

    • Format: gist=<gist id>
    • Example: gist=37c3c97f47a43f20c548
    • Notes: the file should be named Main.purs with the module name Main.
  • Load From URL: Load compressed PureScript code using the code parameter

    • Managed by Try PureScript and updated on editor state change to create shareable URLs
    • Format: code=<compressed string>
    • Example: code=LYewJgrgNgpgBAWQIYEsB2cDuALGAnGIA will set the editor state to the single line module Main where
  • View Mode: Control the view mode using the view parameter

    • Options are: code, output, both (default)
    • Example: view=output will only display the output
  • Auto Compile: Automatic compilation can be turned off using the compile parameter

    • Options are: true (default), false
    • Example: compile=false will turn auto compilation off
  • JavaScript Code Generation: Print the resulting JavaScript code in the output window instead of the output of the program using the js parameter

    • Options are: true, false (default)
    • Example: js=true will print JavaScript code instead of the program's output

Which Libraries Are Available?

Try PureScript aims to provide a complete, recent package set from https://github.com/purescript/package-sets. The available libraries are those listed in staging/spago.dhall, at the versions in the package set mentioned in staging/packages.dhall.

Development

1. Shared setup

These steps should be performed whether you are working on the server, the client, or both.

# Clone into the repository
git clone [email protected]:purescript/trypurescript.git
cd trypurescript

2. Local compile server setup

This step sets up a local server for Try PureScript. You can skip this step if you just want to use the client with the production server.

# Build the trypurescript executable
stack build

# Set up the PureScript environment for the server
cd staging
spago build

# Ensure the compiled JavaScript is available to the client via symbolic link.
ln -s "$PWD/output" "$PWD/../client/public/js/output"

# Then, start the server.
#
# Below, we disable glob expansion via `set -o noglob` to ensure that globs are
# passed to `purs` unchanged.
#
# We run this in a subshell so that setting noglob only lasts for the duration
# of the command and no longer.
(set -o noglob && stack exec trypurescript 8081 $(spago sources))

# Should output that it is compiling the sources (first time)
# Then: Setting phasers to stun... (port 8081) (ctrl-c to quit)

3. Client setup

# Install development dependencies
cd client
npm install

# Use `serve:dev` if you are using a local Try PureScript server,
# e.g. you followed the instructions in step 1.
#
# Use `serve:production` if you would like
# to test the client against the production Try PureScript server.
# Note: the production server may not match the package set you have locally.
npm run serve:(dev|production)

# Try PureScript is now available on localhost:8080

4. Choosing a Tag

The built-in examples for Try PureScript are loaded from this GitHub repository. To change the tag that the examples are loaded from, you'll need to touch three files:

  • client/config/dev/Try.Config.purs
  • client/config/prod/Try.Config.purs
  • client/examples/Main.purs, in the fromExample function.

If you are preparing a release or if you need to adjust examples in development, you should change the tag in these three places (and ensure you're using the same tag in each place!).

Server API

The server is a very basic web service which wraps the PureScript compiler, allowing clients to send PureScript code to be compiled and receiving either compiled JS or error messages in response. It is hosted at https://compile.purescript.org/.

Compile PureScript code

POST /compile

  • Request body: PureScript code defining a module whose name must be Main
  • Status code: 200 (success)

Response body on compilation success:

{
  "js": "...", // a string containing JavaScript code
  "warnings": [ ... ] // an array of warnings, using the same format as the
                      // compiler's --json-errors flag
}

Response body on compilation failure:

{
  "error": {
    "tag": "CompilerErrors",
    "contents": [ ... ] // an array of errors, using the same format as the
                        // compiler's --json-errors flag
  }
}

Response body on other errors (eg, the name of the module in request body was not Main, or the request body was too large)

{
  "error": {
    "tag": "OtherError",
    "contents": "..." // a string containing an error message
  }
}

Note that the API returns a 200 response in all of the above cases; in particular, if the code in the request body fails to compile and the API returns errors, this is still considered a success. Among other things, this makes it easier to use the API from another domain using CORS.

The output code will contain references to any imported modules using require calls. To run these files in the browser, it is necessary to either use a require shim (such as require1k), or replace these calls and deploy a bundle of precompiled modules. The Try PureScript client uses the first approach.

GET /output/:module/(index.js|foreign.js)

The server exposes the compiled JS for all of the modules it has access to. If the compiled JavaScript code in the response includes a require call such as require(../Web.HTML/index.js), then the client is expected to arrange things so that this require call provides access to the JavaScript code available at the URL path /output/Web.HTML/index.js, via a shim or otherwise.

Configuration

The server application takes the following arguments on the command line:

  • port number
  • a list of input source files

Example

trypurescript 8081 'bower_components/purescript-*/src/**/*.purs'

trypurescript's People

Contributors

aloiscochard avatar andys8 avatar drets avatar gabejohnson avatar hdgarrood avatar joneshf avatar jordanmartinez avatar jy14898 avatar liamgoodacre avatar matthieubulte avatar maxdeviant avatar milesfrain avatar monoidmusician avatar natefaubion avatar paf31 avatar pete-murphy avatar rhendric avatar rintcius avatar risto-stevcev avatar sharkdp avatar soupi avatar sudhirvkumar avatar thimoteus avatar thomashoneyman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

trypurescript's Issues

Update UX to be more code focused

Keep the examples and keep the theme consistent with purescript.org, but maybe draw inspiration from the go playground. As you can see most of the space is taken up by the code editor, some by the output window, and a small space at the top for related functions.

I think the examples could be a dropdown in the top bar. Perhaps we could even have a "play.purescript.org" which offers the same functionality of running code and sharing snippets.

Get rid of initial snippets

As discussed in #57 the plan is to get rid of initial snippets. It would be good if the maintainers of the backends would create an initial gist (so that they have ownership of it) & include it here.

After we have all the gists we can get rid of the initial snippets.

@soupi already provided gist for purescript-slides: c62b5778a6a5f2bcd32dd97b294c068a.

Flare backend version

The flare backend appears to be on version 3.0.0.

The bower says it's using 3.2.0:

"purescript-flare": "^3.2.0",
. textarea was added in 3.1.0: https://github.com/sharkdp/purescript-flare/releases/tag/v3.1.0 (and still exists in 3.2.0: https://pursuit.purescript.org/packages/purescript-flare/3.2.0/docs/Flare#v:textarea). But if you try to import textarea, it fails: http://try.purescript.org/?gist=a02503ed103a29e8212811b5140ecda0.

I'm not sure what to do to fix it or I would have sent a PR.

Autocomplete

Ace probably provides an API for this, we just need to expose it on the server.

Provide attribution

We should provide attribution for the libraries we depend on in the Help page.

Try Flare

@sharkdp The process for adding libraries has become much easier since last time we hosted Try Flare. We can also serve the client code from this repo too, like we do with Thermite and purescript-slides if you like. Would you be interested?

cc @garyb too, since we could potentially put together a "Try Halogen" too now.

See #39 for an example of adding a new library.

Use pushState instead of a session redirect

When browsing examples with Firefox the user has to click Back two/three times very quickly to go to the previous page.
In Chrome this works fine with one Back.

Tested with Firefox 52.0a2 (2017-01-24) (64 bit) and Chrome 56.0.2924.76 (64-bit)

Fix Suggestions

The API now returns suggestions for warnings. It would be nice to let the user apply them.

Scrolling past the end

It would be nice if you could scroll past the end in the text editor so that you don't have to bend your neck down when editing the last few lines in a large file. ST3, Vim, Emacs and Visual Studio have similar behavior.

Example:
screenshot

"Slides" Backend Service Not Available

Description

  1. Visit http://try.purescript.org
  2. Select Backend->Slides

Console:

POST https://compile.purescript.org/slides/compile 502 (Bad Gateway)
Failed to load https://compile.purescript.org/slides/compile: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 502.

Extract client code into a plugin

After some discussion with Phil about how we could improve documentation for PureScript libraries, we arrived at a design where we would like to have GitHub Pages-hosted documents easily embed runnable PureScript snippets, with something like script or pre tags, and a plugin that adds a "Run" button, posts the code to the Try PS API when the user clicks, and finally renders the output (below the snippet).

I have not had time to look into how this code is doing stuff right now, just throwing this out there. ๐Ÿ˜„

Thx!

Not clear how to get dist/

Hey, thanks for this first of all!

I am having some problems getting to the point where I can run the example command you provided.

$ cabal --version gives me

cabal-install version 1.22.9.0
using version 1.22.8.0 of the Cabal library

I had to install the following packages

$ cabal install glob aeson http-types purescript scotty

Then I've run

$ cabal install
# takes quite some time, then I clearly see it doing
Downloading purescript-0.10.5...
Configuring purescript-0.10.5...
Building purescript-0.10.5...
# some more output

When I execute $ cabal run I get

Preprocessing executable 'trypurescript' for trypurescript-0.10.5...
Linking dist/build/trypurescript/trypurescript ...
Running trypurescript...
trypurescript: user error (Pattern match failure in do expression at server/Main.hs:164:3-27)

I know next to nothing about Haskell or PureScript, any help would be greatly appreciated!

UPDATE

I figured out I don't need to run cabal run, I just have to run the command from dist/, nevermind, are you willing to take a PR with some detailed instructions on how to use this from a newbie point of view?

Use servant

Just a thought. It would let us generate a client and documentation.

Add Data.Record

The current package-set used by TryPurescript is psc-0.11.6, if I'm reading correctly. That package-set doesn't have include "records", but it is in master, so we'll have to wait until that package-set gets a new tag, then we can send a PR here to update the package-set and add record.

Unable to create a gist

When attempting to share, I receive an alert with the message:

From try.purescript.org
Failed to create gist

This is caused by a failed POST:

Request URL: https://api.github.com/gists
Request Method: POST
Status Code: 422 Unprocessable Entity

The response payload:

{
  "message": "Validation Failed",
  "errors": [
    {
      "resource": "Gist",
      "code": "missing_field",
      "field": "user"
    },
    {
      "resource": "Gist",
      "code": "missing_field",
      "field": "files"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}

Flare CSS problems

@sharkdp First of all I wanted to say flare still looks awesome!

I have noticed a few CSS problems in the try purescript site:

  1. in the temperature example the temperature picker goes over the temperature label (probably because the width for it is 100px). Perhaps a putting the components in a table will work?
  2. Components like the font size for the title or hue and radius in the drawing example are invisible to me. I can manipulate them if i press where they are supposed to be but i can't see them.

This is using Firefox 50.

edit: I can, however, see the sliders for hue and radius fine on my phone (also firefox).

Use React?

I'd like to consider this so that we can add more interesting components more easily over time.

The one I'm interested in right now is adding a new panel with a list of errors and warnings.

Update to 0.12

I'd like to take this on, but it should probably wait until the full release (as in after release-candidate period), and there's a non-rc 0.12 package set.

EDIT:

Looking at this more, it seems some dependencies will need to be updated as well. This is a possibly nonexhaustive list:

  • thermite and its dependencies:
    • aff
    • coroutines
    • dom
    • freet
    • profunctor-lenses
    • react-dom
    • react
  • behaviors and its dependencies:
    • sets ordered-collections
    • filterable
    • nullable
    • drawing
    • effect
  • flare (sparkle) and its dependencies:
    • const
    • exists
    • free
    • graphs deprecated, replaced by?
    • profunctor
    • quickcheck
    • semirings
    • validation
  • mathbox and its dependencies:

Add Aff to Core backend?

I found myself wanting to typecheck an Aff code snippet. TryPS doesn't have Aff in the core package-set, which I find a little odd, as several libraries/UI frameworks depend on it.

Button to toggle auto compile

While a phenomenal feature, I wrote some laggy synchronous code that intermittently froze the editor when I typed too slow. Alternatively a longer delay might help e.g. half a second.

iOS: code pane cut off

When using iOS to view try.purescript.org the bottom of the coding pane is cut off at the bottom of the viewport, if there's more code than will fit in the pane it is cut off and no vertical scroll bar is displayed, scrolling does not work.

Additionally, tapping to set the focus in the box works (as does typing normal characters into that box), however the arrow keys on an iPad keyboard (physical) do not function: pressing them has no effect.

Port to PureScript

I think we have enough JS code here now that we should port it to PureScript.

Save code buffer

Several times I've refreshed the page (Super+R) instead of opening a new tab (Super+T) ๐Ÿ˜ญ

  • Easy fix: ask whether to navigate away from an edited buffer.
  • Nice fix: keep active buffer in local storage.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.