Skip to content

Commit

Permalink
Merge branch 'feature/explicit_unicode_support#1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ne-sachirou committed Oct 18, 2015
2 parents eaf37b5 + 4cd851a commit 488f83d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Cucumber::Rake::Task.new :features do |t|
end

task :build do
sh 'stack build'
sh 'hlint . -c'
sh 'stack build -j4'
FileUtils.mkdir 'bin' unless File.exist? 'bin'
FileUtils.cp `stack exec which private-values`.strip, 'bin', preserve: true
readme = Erubis::Eruby.new(File.read("#{__dir__}/src/README.md.erb", mode: 'r:utf-8')).result({
help: File.read("#{__dir__}/src/Help.txt", mode: 'r:utf-8').strip,
Expand Down
9 changes: 9 additions & 0 deletions features/get_command.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ Feature: Get command
And run GET command with an option "someProject.float1"
Then the output should contain exactly "42.0"

Scenario: Get Unicode keys and values
Given a project named "someProject"
And run SET command with options "someProject.this-value" and "この値"
And run GET command with an option "someProject.this-value"
Then the output should contain exactly "この値"
Given run SET command with options "someProject.その値" and "'That value'"
And run GET command with an option "someProject.その値"
Then the output should contain exactly "That value"

Scenario: Get a value, but the project isn't exist
Given run GET command with an option "someProject.key"
Then it should fail with:
Expand Down
11 changes: 11 additions & 0 deletions features/keys_command.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ Feature: Keys command
str2
"""

Scenario: List Unicode keys
Given a project named "someProject"
And run SET command with options "someProject.this-value" and "この値"
And run SET command with options "someProject.その値" and "'That value'"
And run KEYS command with an option "someProject"
Then it should pass with exactly:
"""
this-value
その値
"""

Scenario: List keys of a project, but the project isn't exist
Given run KEYS command with an option "someProject"
Then it should fail with:
Expand Down
10 changes: 10 additions & 0 deletions features/set_command.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ Feature: Set command
white space2: value
"""

Scenario: Set Unicode keys and values
Given a project named "someProject"
And run SET command with options "someProject.this-value" and "この値"
And run SET command with options "someProject.その値" and "'That value'"
Then the file "~/.private-values/someProject/values.yml" should contain:
"""
その値: That value
this-value: この値
"""

Scenario: Set a value, but the project isn't exist
Given run SET command with options "someProject.key" and "value"
Then it should fail with:
Expand Down
4 changes: 4 additions & 0 deletions private-values.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ library
, regex-tdfa >= 1.2
, split >= 0.2
, template-haskell >= 2.10
, text >= 1.2
, utf8-string >= 1.0
, yaml >= 0.8
, yaml-light >= 0.1
default-language: Haskell2010
Expand All @@ -42,6 +44,8 @@ executable private-values
, regex-tdfa
, split
, template-haskell
, text
, utf8-string
, yaml
, yaml-light
, private-values
Expand Down
8 changes: 5 additions & 3 deletions src/Lib.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE QuasiQuotes, OverloadedStrings #-}
module Lib
( cmdProjects
, cmdNew
Expand All @@ -10,10 +10,11 @@ module Lib
, cmdHelp
) where

import Data.List ( intercalate )
import Data.List ( intercalate )
import Data.List.Split ( splitOn )
import Literal ( literalFile )
import Literal ( literalFile )
import Project
import System.IO ( hPutStr, hSetEncoding, stdout, utf8 )

cmdProjects :: [String] -> IO ()
cmdProjects args =
Expand Down Expand Up @@ -69,6 +70,7 @@ cmdGet args =
project <- initProject projectName
shouldExist project
value <- getValue project key
hSetEncoding stdout utf8
putStr value

cmdHelp :: IO ()
Expand Down
23 changes: 13 additions & 10 deletions src/Project.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module Project where

import Data.Map.Lazy ( insert, keys )
import Data.Yaml ( encodeFile )
import Data.Yaml.YamlLight ( parseYamlFile )
import Codec.Binary.UTF8.String ( encodeString )
import Control.Monad ( unless )
import Data.Map.Lazy ( insert, keys )
import qualified Data.Text.Encoding as Text
import qualified Data.Text.IO as Text
import Data.Yaml ( encode )
import Data.Yaml.YamlLight ( parseYaml, parseYamlFile )
import FileUtil
import System.Directory
import System.FilePath
import System.FilePath ( takeBaseName )
import System.IO
import Text.Regex.TDFA
import YamlUtil
Expand All @@ -26,7 +30,7 @@ getProjectConfig =
config <- parseYamlFile $ homePath ++ "/private-values.rc"
valuesDir <- absolutize $ getValueFromYL "values-dir" config
return $ ProjectConfig valuesDir
else do
else
return $ ProjectConfig (homePath ++ "/.private-values")

data Project = Project { config :: ProjectConfig, name :: String }
Expand All @@ -50,9 +54,7 @@ shouldExist project =
let Project _ name = project
in do
isExist <- doesDirectoryExist $ path project
case isExist of
False -> fail $ "The project \"" ++ name ++ "\" isn't exist.\nRun `private-values new " ++ name ++ "`."
True -> return ()
unless isExist $ fail $ "The project \"" ++ name ++ "\" isn't exist.\nRun `private-values new " ++ name ++ "`."

create :: Project -> IO ()
create project =
Expand All @@ -78,9 +80,10 @@ setValue project key value =
let valuesPath = path project ++ "/values.yml"
in do
yValues <- parseYamlFile valuesPath
encodeFile valuesPath $ insert key value $ toMapFromYL yValues
let yaml = Text.decodeUtf8 $ encode $ insert key value $ toMapFromYL yValues
Text.writeFile valuesPath yaml

getValue :: Project -> String -> IO String
getValue project key =
do values <- parseYamlFile $ path project ++ "/values.yml"
return $ getValueFromYL key values
return $ getValueFromYL (encodeString key) values
13 changes: 7 additions & 6 deletions src/YamlUtil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ module YamlUtil
, getValueFromYL
) where

import Data.ByteString ( ByteString )
import Data.ByteString ( ByteString )
import Data.Map.Lazy as Map ( Map, fromList, map, mapKeys )
import Data.Maybe ( fromMaybe )
import Data.String ( fromString )
import Data.Yaml.YamlLight ( YamlLight ( YStr ), lookupYL, unMap, unStr )
import Data.Maybe ( maybe )
import Data.String ( fromString )
import qualified Data.Text.Encoding as Text
import Data.Yaml.YamlLight ( YamlLight ( YStr ), lookupYL, unMap, unStr )

toStringFromYL :: YamlLight -> String
toStringFromYL value = fromMaybe "" $ fmap toStrFromByteStr $ unStr value
toStringFromYL value = maybe "" toStrFromByteStr $ unStr value

toMapFromYL :: YamlLight -> Map String String
toMapFromYL yValues =
Expand All @@ -28,4 +29,4 @@ getValueFromYL key values =
Just value -> toStrFromByteStr value

toStrFromByteStr :: ByteString -> String
toStrFromByteStr byteStr = read $ show byteStr
toStrFromByteStr byteStr = read $ show (Text.decodeUtf8 byteStr)

0 comments on commit 488f83d

Please sign in to comment.