Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load docs offline #235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/Analyser/DependencyHandler.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
port module Analyser.DependencyHandler exposing (CacheDependencyRead(..), DependencyPointer, loadDependencyFiles, loadOnlineDocumentation, onLoadDependencyFilesFromDisk, onOnlineDocumentation, onReadFromDisk, readFromDisk, storeToDisk)
port module Analyser.DependencyHandler exposing (CacheDependencyRead(..), DependencyPointer, loadDependencyFiles, loadDocsJson, onDocsLoaded, onLoadDependencyFilesFromDisk, onReadFromDisk, readFromDisk, storeToDisk)

import Analyser.FileContext as FileContext
import Analyser.Files.FileContent as FileContent exposing (FileContent)
Expand Down Expand Up @@ -46,7 +46,7 @@ type alias DependencyFiles =
}


type alias HttpDocumentationLoad =
type alias DocsJsonLoad =
{ dependency : DependencyPointer
, json : Value
}
Expand Down Expand Up @@ -101,20 +101,15 @@ onReadFromDisk { name, version } =
-- Online Docs Ports


port loadHttpDocumentation : DependencyPointer -> Cmd msg
port loadDocsJson : DependencyPointer -> Cmd msg


port onHttpDocumentation : (HttpDocumentationLoad -> msg) -> Sub msg
port onDocsJsonLoaded : (DocsJsonLoad -> msg) -> Sub msg


loadOnlineDocumentation : DependencyPointer -> Cmd msg
loadOnlineDocumentation =
loadHttpDocumentation


onOnlineDocumentation : DependencyPointer -> Sub (Maybe (Result JD.Error Dependency))
onOnlineDocumentation dep =
onHttpDocumentation
onDocsLoaded : DependencyPointer -> Sub (Maybe (Result JD.Error Dependency))
onDocsLoaded dep =
onDocsJsonLoaded
(\{ dependency, json } ->
if dependency == dep then
JD.decodeValue (JD.list Elm.Docs.decoder) json
Expand Down
16 changes: 8 additions & 8 deletions src/Analyser/Files/DependencyLoader.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ type alias Model =

type State
= AwaitingCache
| LoadingOnlineDocs
| LoadingDocs
| RawDiskLoading
| Failure
| Done Dependency


type Msg
= OnCacheRead CacheDependencyRead
| OnOnlineDocs (Maybe (Result JD.Error Dependency))
| OnDocsLoaded (Maybe (Result JD.Error Dependency))
| OnLocallyBuildDependency (Maybe (Result String Dependency))


Expand Down Expand Up @@ -70,16 +70,16 @@ update msg model =
)

Failed ->
( { model | state = LoadingOnlineDocs }
, DependencyHandler.loadOnlineDocumentation model.dependency
( { model | state = LoadingDocs }
, DependencyHandler.loadDocsJson model.dependency
)

Ignore ->
( model
, Cmd.none
)

OnOnlineDocs result ->
OnDocsLoaded result ->
case result of
Nothing ->
( model, Cmd.none )
Expand Down Expand Up @@ -129,9 +129,9 @@ subscriptions model =
Done _ ->
Sub.none

LoadingOnlineDocs ->
DependencyHandler.onOnlineDocumentation model.dependency
|> Sub.map OnOnlineDocs
LoadingDocs ->
DependencyHandler.onDocsLoaded model.dependency
|> Sub.map OnDocsLoaded

RawDiskLoading ->
DependencyHandler.onLoadDependencyFilesFromDisk model.dependency
Expand Down
6 changes: 3 additions & 3 deletions ts/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface DependencyStore {
dependency: DependencyPointer;
content: string;
}
export interface HttpDocumentationLoad {
export interface DocsJsonLoad {
dependency: DependencyPointer;
json: any;
}
Expand Down Expand Up @@ -63,7 +63,7 @@ interface ElmApp {
storeAstForSha: Subscription<AstStore>;
storeFile: Subscription<FileStore>;
loadFileContentWithSha: Subscription<string>;
loadHttpDocumentation: Subscription<DependencyPointer>;
loadDocsJson: Subscription<DependencyPointer>;
storeRawDependency: Subscription<DependencyStore>;
loadRawDependency: Subscription<DependencyPointer>;

Expand All @@ -76,7 +76,7 @@ interface ElmApp {
fileContent: MailBox<FileContent>;
onStoredFiles: MailBox<boolean>;
onFileContentWithShas: MailBox<FileContentSha>;
onHttpDocumentation: MailBox<HttpDocumentationLoad>;
onDocsJsonLoaded: MailBox<DocsJsonLoad>;
onRawDependency: MailBox<RawDependencyLoad>;
};
}
Expand Down
4 changes: 2 additions & 2 deletions ts/file-loading-ports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ElmApp, Config } from './domain';

import * as DependencyFiles from './ports/dependency-files';
import * as RawDependencies from './ports/raw-dependencies';
import * as HttpDocumentation from './ports/http-documentation';
import * as DocsJsonLoader from './ports/docs-json-loader';
import * as FileLoader from './ports/file-loader';
import * as Context from './ports/context';
import { LocalCache } from './util/cache';
Expand All @@ -12,7 +12,7 @@ export function setup(app: ElmApp, config: Config, directory: string): void {
const localCache = new LocalCache(directory);
const fileReader = new FileReader(localCache);

HttpDocumentation.setup(app);
DocsJsonLoader.setup(app, directory);
RawDependencies.setup(app);
DependencyFiles.setup(app, directory, fileReader);
FileLoader.setup(app, config, directory, localCache, fileReader);
Expand Down
49 changes: 49 additions & 0 deletions ts/ports/docs-json-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as request from 'request';
import * as path from 'path';
import * as os from 'os';

import { ElmApp, DependencyPointer } from '../domain';

function loadFromPackageSite({ name, version }: DependencyPointer, cb: (result: any) => void) {
request.get(`http://package.elm-lang.org/packages/${name}/${version}/docs.json`, function(
err: any,
_response: request.Response,
body: string
) {
if (err) {
cb(null);
}
try {
const parsed = JSON.parse(body);
cb(parsed);
} catch (e) {
cb(null);
}
});
}

function loadFromElmHome({ name, version }: DependencyPointer, directory: string) {
const projectElmJson = path.resolve(directory, 'elm.json');
const elmVersion = require(projectElmJson)['elm-version'];
const elmHome = process.env.ELM_HOME || path.resolve(os.homedir(), '.elm');
const docsJsonPath = path.resolve(elmHome, elmVersion, 'package', name, version, 'docs.json');
return require(docsJsonPath);
}

function setup(app: ElmApp, directory: string) {
app.ports.loadDocsJson.subscribe((pointer: DependencyPointer) => {
const onLoaded = (json: any) =>
app.ports.onDocsJsonLoaded.send({
dependency: pointer,
json
});
try {
const docs = loadFromElmHome(pointer, directory);
onLoaded(docs);
} catch (e) {
loadFromPackageSite(pointer, onLoaded);
}
});
}

export { setup };
36 changes: 0 additions & 36 deletions ts/ports/http-documentation.ts

This file was deleted.