Skip to content

Instantly share code, notes, and snippets.

@lshifr
Created December 18, 2012 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lshifr/4329721 to your computer and use it in GitHub Desktop.
Save lshifr/4329721 to your computer and use it in GitHub Desktop.
An object to manage project.m files for Github gist-based Mathematica projects
(* Mathematica Package *)
BeginPackage["GithubGistProjectInfo`", { "RuleTreeInfo`", "OO`", "OO`Methods`", "OO`Errors`"}]
(* Exported symbols added here with SymbolName::usage *)
GithubGistProjectInfo;
Begin["`Private`"] (* Begin Private Context *)
join = Function[{dir, file}, FileNameJoin[{dir, file}]];
DeclareType[GithubGistProjectInfo ~ Extends ~ RuleTreeInfo][
OO`Methods`newProjectQ[] :=
$self@OO`Methods`get["url"] === $Failed
,
OO`Methods`urlValidQ[] :=
$self@OO`Methods`get["gistID"] =!= $Failed
,
OO`Methods`get["gistID"] :=
With[{ids =
StringCases[
$self@OO`Methods`get["url"],
"https://gist.github.com/" ~~ (n : NumberString) :> n
]},
First[ids] /; Length[ids] == 1
]
,
OO`Methods`get["gistID"] := $Failed
,
OO`Methods`get["description"] /; ! TrueQ[inPrinfoGet] :=
Block[{inPrinfoGet = True},
If[# === $Failed, Automatic, #] &[
$self@OO`Methods`get["description"]
]]
,
OO`Methods`verifyContent[] /; And[
! $self@OO`Methods`newProjectQ[],
! $self@OO`Methods`urlValidQ[]
] :=
ThrowError[GithubGistProjectInfo, OO`Methods`verifyContent,
"invalid_project_url"]
,
OO`Methods`verifyContent[] :=
With[{result = Quiet@ExportString[$content, "JSON"]},
$self /; result =!= $Failed && MatchQ[$content, {__Rule} | _Rule]
]
,
OO`Methods`verifyContent[___] :=
ThrowError[GithubGistProjectInfo, OO`Methods`verifyContent]
,
OO`Methods`new[finfo_?(TypeQ[FileInfo`FileInfo]), opts___?OptionQ] :=
Module[{changes},
changes =
"ProjectChanges" /. Flatten[{opts}] /. "ProjectChanges" -> {};
(* We need to refer to FileInfo methods via the long names,
since explicit dependency on FileInfo has not been added *)
$self@bindToFile[finfo@OO`Methods`getDir[]~join~"project.m"];
$self@read[];
$self@verifyContent[];
$self@replace[changes];
$self@verifyContent[];
$self
]
];
End[] (* End Private Context *)
EndPackage[]
{
"author"->
{
"name" -> "Leonid Shifrin",
"email" -> "lshifr@gmail.com",
"url" -> "http://www.mathprogramming-intro.org"
},
"name" -> "GithubGistProjectInfo",
"mathematica_version" -> "8.0+",
"description" -> "An object to manage project.m files for Github gist-based Mathematica projects",
"url" -> "https://gist.github.com/4329721"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment